Skip to content

Commit 3bcda50

Browse files
Googlera-maurice
authored andcommitted
As per the thread in Firebase for Games, I'm updating the sample to work verbatim. I've made the following changes:
I've replaced printf with std::cout (this is more C++, and should avoid code safety warnings on some compilers). Rather than spinning on sign_in_future.status() and calling a non-existent function Wait, I register an OnCompletion listener as a lambda that captures no scope (for performance optimists out there, may avoid a heap allocation). I made the type of the future explicit (the code as is wouldn't compile). It would read better as auto, but I think having a full type here is better for educational purposes. All code after the spin lock-ish pattern is moved into the lambda. I changed user->Anonymous() (doesn't exist) to user->is_anonymous(). I did my best to match the c++ style guide in the comment, but I expect that there'll be a critique or two. I have verified the code working in a minimalist sample application. I've just grabbed the suggested reviewers, sorry if this is out of nowhere! PiperOrigin-RevId: 259400353
1 parent 463e7c8 commit 3bcda50

File tree

1 file changed

+25
-20
lines changed
  • auth/src/include/firebase

1 file changed

+25
-20
lines changed

auth/src/include/firebase/auth.h

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,33 @@ class PhoneAuthProvider;
9191
///
9292
/// For example:
9393
/// @code{.cpp}
94-
/// // Get the Auth class for your App.
95-
/// firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
9694
///
97-
/// // Request anonymous sign-in and wait until asynchronous call completes.
98-
/// firebase::Future<firebase::auth::User*> sign_in_future =
99-
/// auth->SignInAnonymously();
100-
/// while (sign_in_future.status() == firebase::kFutureStatusPending) {
101-
/// Wait(100);
102-
/// printf("Signing in...\n");
103-
/// }
95+
/// // Get the Auth class for your App.
96+
/// firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
10497
///
105-
/// // Print sign in results.
106-
/// const firebase::auth::AuthError error =
107-
/// static_cast<firebase::auth::AuthError>(sign_in_future.error());
108-
/// if (error != firebase::auth::kAuthErrorNone) {
109-
/// printf("Sign in failed with error `%s`\n",
110-
/// sign_in_future.error_message());
111-
/// } else {
112-
/// firebase::auth::User* user = *sign_in_future.result();
113-
/// printf("Signed in as %s user.\n",
114-
/// user->Anonymous() ? "an anonymous" : "a non-anonymous");
115-
/// }
98+
/// // Request anonymous sign-in and wait until asynchronous call completes.
99+
/// firebase::Future<firebase::auth::User*> sign_in_future =
100+
/// auth->SignInAnonymously();
101+
/// while(sign_in_future.status() == firebase::kFutureStatusPending) {
102+
/// // when polling, like this, make sure you service your platform's
103+
/// // message loop
104+
/// // see https://github.com/firebase/quickstart-cpp for a sample
105+
/// ProcessEvents(300);
106+
/// std::cout << "Signing in...\n";
107+
/// }
108+
///
109+
/// const firebase::auth::AuthError error =
110+
/// static_cast<firebase::auth::AuthError>(sign_in_future.error());
111+
/// if (error != firebase::auth::kAuthErrorNone) {
112+
/// std::cout << "Sign in failed with error '"
113+
/// << sign_in_future.error_message() << "'\n";
114+
/// } else {
115+
/// firebase::auth::User* user = *sign_in_future.result();
116+
/// // is_anonymous from Anonymous
117+
/// std::cout << "Signed in as "
118+
/// << (user->is_anonymous() ? "an anonymous" : "a non-anonymous")
119+
/// << " user\n";
120+
/// }
116121
/// @endcode
117122
/// @endif
118123
class Auth {

0 commit comments

Comments
 (0)