You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app.
Before you begin
Set up your Unity project
Before you can use Firebase Authentication, you need to:
Register your Unity project and configure it to use Firebase.
If your Unity project already uses Firebase, then it's already registered and configured for Firebase.
If you don't have a Unity project, you can download a sample app.
Add the Firebase Unity SDK (specifically,
FirebaseAuth.unitypackage) to your Unity project.
Note that adding Firebase to your Unity project involves tasks both in the Firebase console and in your open Unity project (for example, you download Firebase config files from the console, then move them into your Unity project).
Set up your Firebase project and credentials
Android: Specify your app's SHA-1 fingerprint in the Firebase console:
- In the Firebase console, go to
Settings > General tab. Go to the Your apps card, select your Android app, and add your SHA-1 fingerprint in the SHA certificate fingerprints field.
You can retrieve your keystore's SHA-1 fingerprint using
keytool:keytool -keystore path-to-keystore.keystore -list -vSee Authenticating Your Client for more details on getting your app's SHA fingerprint.
- In the Firebase console, go to
Enable Google Sign-In and copy your Web client ID:
- In the Firebase console, go to Security > Authentication.
- In the Sign-in method tab, enable the Google sign-in provider.
- Expand the Web SDK configuration section and copy your Web client
ID (for example,
123456-abc.apps.googleusercontent.com). This represents the audience (aud) claim of your ID tokens and is required for both Android and iOS. - Click Save.
Update your Firebase config files and copy your iOS reversed client ID:
- Download the latest Firebase configuration files from the
Firebase console (
for Android andgoogle-services.json for iOS) and replace any older copies in your Unity project. (See Add Firebase to your Unity project.)GoogleService-Info.plist - iOS: Open your
file and copy the value of theGoogleService-Info.plist REVERSED_CLIENT_IDkey (for example,com.googleusercontent.apps.12345-abc). This value is required to configure the iOS redirect URL scheme.
- Download the latest Firebase configuration files from the
Firebase console (
Integrate the Google Sign-In plugin into your Unity project
Import the plugin:
- If you are integrating into an existing project, back up your project and remove any older Google Sign-In plugins before proceeding.
- Download
GoogleSignInNative.unitypackagefrom GitHub and import it into your Unity project (Assets > Import Package > Custom Package). Ensure all files are selected and click Import.
Configure plugin settings:
- In the Unity Editor, go to Window > Google Sign-In Native > Settings.
- Paste your Web Client ID into the first field.
- Paste your iOS Reversed Client ID into the second field.
- Click Save Settings.
Resolve native dependencies:
- Go to Assets > External Dependency Manager > Android Resolver > Force Resolve.
- Wait for the success message. This downloads the native
googleidandandroidxCredential Manager libraries.
Access the Firebase.Auth.FirebaseAuth class
The FirebaseAuth class is the gateway for all API calls.
It is accessible through FirebaseAuth.DefaultInstance.
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Authenticate with Firebase
Set up
GoogleSignInManagerin your scene:- Create an empty
GameObjectin your initial scene namedGoogleSignInManager. - Attach the
GoogleSignInManager.csscript to it. - Drag the
GoogleSignInNativeSettingsasset (located inAssets/GoogleSignInNative/Resources) into the Sign In Settings slot on the script.
- Create an empty
Trigger Google Sign-In and exchange the ID token for a Firebase credential:
Create a controller script that subscribes to
GoogleSignInManager.OnSignInCompletedandGoogleSignInManager.OnSignInFailed, triggers sign-in withGoogleSignInManager.Instance.StartSignIn(), and exchanges the resulting Google ID token for a Firebase credential usingGoogleAuthProvider.GetCredential:using Firebase.Auth; using Firebase.Extensions; using UnityEngine; public class MyAuthController : MonoBehaviour { private FirebaseAuth auth; private void Awake() { auth = FirebaseAuth.DefaultInstance; } private void OnEnable() { GoogleSignInManager.OnSignInCompleted += HandleSignInSuccess; GoogleSignInManager.OnSignInFailed += HandleSignInFailure; } private void OnDisable() { GoogleSignInManager.OnSignInCompleted -= HandleSignInSuccess; GoogleSignInManager.OnSignInFailed -= HandleSignInFailure; } public void OnSignInButtonClick() { GoogleSignInManager.Instance.StartSignIn(); } private void HandleSignInSuccess(string idToken) { Debug.Log("[Auth] Google Sign-In succeeded. Authenticating with Firebase..."); Credential credential = GoogleAuthProvider.GetCredential(idToken, null); auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWithOnMainThread(task => { if (task.IsCanceled) { Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " + task.Exception); return; } AuthResult result = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", result.User.DisplayName, result.User.UserId); }); } private void HandleSignInFailure(string error) { Debug.LogError($"[Auth] Google Sign-In failed: {error}"); } }
Build your app
Android
- Ensure your Package Name in Player Settings matches your Firebase and Google Cloud OAuth credentials exactly.
- Ensure your Minimum API Level is set to 34 or higher.
- Click Build and Run.
iOS
- Build your project in Unity to generate an Xcode project, then open the
generated
.xcworkspacefile. - In Xcode, go to the Signing & Capabilities tab and select your Development Team.
- The plugin automatically adds the custom URL scheme (
REVERSED_CLIENT_ID) to your Xcode project. Build and run on a physical device.
Next steps
After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.
-
In your apps, you can get the user's basic profile information from the
Firebase.Auth.FirebaseUserobject:Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { string name = user.DisplayName; string email = user.Email; System.Uri photo_url = user.PhotoUrl; // The user's Id, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, if you // have one; use User.TokenAsync() instead. string uid = user.UserId; }
In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the signed-in user's unique user ID from the
authvariable, and use it to control what data a user can access.
You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account.
To sign out a user, call
SignOut():
auth.SignOut();