Get Started with Firebase Authentication in C++

You can use Firebase Authentication to allow users to sign in to your app using one
or more sign-in methods, including email address and password sign-in, and
federated identity providers such as Google Sign-in and Facebook Login. This
tutorial gets you started with Firebase Authentication by showing you how to add
email address and password sign-in to your app.

Note that adding Firebase to your C++ project involves tasks both in the
Firebase console and in your open C++ project (for example, you download
Firebase config files from the console, then move them into your C++ project).

Sign up new users

Create a form that allows new users to register with your app using their
email address and a password. When a user completes the form, validate the
email address and password provided by the user, then pass them to the
CreateUserWithEmailAndPassword method:

You can check the status of the account creation operation either by registering
a callback on the CreateUserWithEmailAndPasswordLastResult Future object, or,
if you're writing a game or app with some kind of periodic update loop, by
polling the status in the update loop.

For example, using a Future:

firebase::Future<firebase::auth::User*> result =
auth->CreateUserWithEmailAndPasswordLastResult();
// The lambda has the same signature as the callback function.
result.OnCompletion(
[](const firebase::Future<firebase::auth::User*>& result,
void* user_data) {
// `user_data` is the same as &my_program_context, below.
// Note that we can't capture this value in the [] because std::function
// is not supported by our minimum compiler spec (which is pre C++11).
MyProgramContext* program_context =
static_cast<MyProgramContext*>(user_data);
// Process create user result...
(void)program_context;
},
&my_program_context);

Or, to use polling, do something like the following example in your game's
update loop:

Get the result of the sign-in operation the same way you got the sign-up result.

Set an authentication state listener and get account data

To respond to sign-in and sign-out events, attach a listener to the global
authentication object. This listener gets called whenever the user's sign-in
state changes. Because the listener runs only after the authentication object is
fully initialized and after any network calls have completed, it is the best
place to get information about the signed-in user.

Create the listener by implementing the firebase::auth::AuthStateListener
abstract class. For example, to create a listener that gets information about
the user when a user successfully signs in: