Get Started with Firebase Authentication in C++ Stay organized with collections Save and categorize content based on your preferences.
You can useFirebase Authentication to allow users to sign in to your app using oneor more sign-in methods, including email address and password sign-in, andfederated identity providers such as Google Sign-in and Facebook Login. Thistutorial gets you started withFirebase Authentication by showing you how to addemail address and password sign-in to your app.
Connect your C++ project to Firebase
Before you can useFirebase Authentication,you need to:
Register your C++ project and configure it to use Firebase.
If your C++ project already uses Firebase, then it's already registered andconfigured for Firebase.
Add theFirebaseC++ SDK to your C++ project.
Note that adding Firebase to your C++ project involves tasks both in theFirebase console and in your open C++ project (for example, you downloadFirebase 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 theiremail address and a password. When a user completes the form, validate theemail address and password provided by the user, then pass them to theCreateUserWithEmailAndPassword method:
firebase::Future<firebase::auth::AuthResult>result=auth->CreateUserWithEmailAndPassword(email,password);You can check the status of the account creation operation either by registeringa callback on theCreateUserWithEmailAndPasswordLastResult Future object, or,if you're writing a game or app with some kind of periodic update loop, bypolling the status in the update loop.
For example, using a Future:
firebase::Future<firebase::auth::AuthResult>result=auth->CreateUserWithEmailAndPasswordLastResult();// The lambda has the same signature as the callback function.result.OnCompletion([](constfirebase::Future<firebase::auth::AuthResult>&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'supdate loop:
firebase::Future<firebase::auth::AuthResult>result=auth->CreateUserWithEmailAndPasswordLastResult();if(result.status()==firebase::kFutureStatusComplete){if(result.error()==firebase::auth::kAuthErrorNone){firebase::auth::AuthResult*auth_result=*result.result();printf("Create user succeeded for email %s\n",auth_result.user.email().c_str());}else{printf("Created user failed with error '%s'\n",result.error_message());}}Sign in existing users
Create a form that allows existing users to sign in using their email addressand password. When a user completes the form, call theSignInWithEmailAndPassword method:
firebase::Future<firebase::auth::AuthResult>result=auth->SignInWithEmailAndPassword(email,password);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 globalauthentication object. This listener gets called whenever the user's sign-instate changes. Because the listener runs only after the authentication object isfully initialized and after any network calls have completed, it is the bestplace to get information about the signed-in user.
Create the listener by implementing thefirebase::auth::AuthStateListenerabstract class. For example, to create a listener that gets information aboutthe user when a user successfully signs in:
classMyAuthStateListener:publicfirebase::auth::AuthStateListener{public:voidOnAuthStateChanged(firebase::auth::Auth*auth)override{firebase::auth::Useruser=auth.current_user();if(user.is_valid()){// User is signed inprintf("OnAuthStateChanged: signed_in %s\n",user.uid().c_str());conststd::stringdisplayName=user.DisplayName();conststd::stringemailAddress=user.Email();conststd::stringphotoUrl=user.PhotoUrl();}else{// User is signed outprintf("OnAuthStateChanged: signed_out\n");}// ...}};Attach the listener with thefirebase::auth::Auth object'sAddAuthStateListener method:
MyAuthStateListenerstate_change_listener;auth->AddAuthStateListener(&state_change_listener);Next steps
Learn how to add support for other identity providers and anonymous guestaccounts:
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.