Google Sign-In¶
The purpose of this guide is to help game makers use Google Sign-In with the Beamable Accounts feature.
Beamable integrates with Google's Sign-In service to provide seamless authentication for your game. Google's Sign-In manages the OAuth 2.0 flow and token lifecycle, simplifying your integration with Google APIs. A user always has the option to revoke access to an application at any time.
Getting Started¶
This guide provides step-by-step instructions to set up Google Sign-In with Beamable's Accounts feature in a Unity project.
Google Play Game Services
This guide covers Google Sign-In using OAuth. It does not apply to Google Play Game Services (GPGS), which uses a different authentication flow.
Create a Unity Project¶
Set up the Beamable SDK for Unity
- See Beamable's Getting Started for more info
Switch platform to Android
- Unity → File → Build Settings
- Select Android, then press Switch Platform
Set Package Name
- Unity → File → Build Settings → Player Settings
- Set your package name (e.g.,
com.yourcompany.yourgame) - You will need this exact package name for the Google Cloud Console setup
Create KeyStore for Signing¶
Create a KeyStore
- Go to Unity → File → Build Settings → Player Settings
- In Inspector, go to Publishing Settings and create the keystore (or unlock an existing one)
- Important: Save the password securely
Record the KeyStore SHA-1 Fingerprint
- Open a terminal where you created the keystore and run:
1keytool -list -v -keystore <your.keystore> - Look for the SHA-1 fingerprint in the output and save it
- If you don't have
keytoolinstalled, follow the instructions after installing Java on your machine: Here
Create Google Cloud Console - OAuth 2.0 Credentials¶
Create a Project & Enable APIs:
- Go to Google Cloud Console
- Create OR select an existing project
- Go to APIs & Services → Enable APIs and Services
- Enable the following API: Google+ API
Create Credentials:
Go to API & Services → Credentials → Create Credentials → OAuth client ID
You need to create TWO credentials:
Web Application Credentials:
- Select "Web application" as the application type
- Give it a name
- Important: Save the Client ID and Client Secret - you will need these later
Android Credentials:
- Select "Android" as the application type
- Enter your exact package name (from Step 1.3)
- Enter the SHA-1 fingerprint (from Step 2.2)
- Save the Client ID just in case it is necessary later
Important
You will need both Web application credentials AND platform-specific credentials for Android or iOS or both.
Configure Unity Project¶
Beamable's Google Sign-In integration requires play-services-auth in your Android build.
Enable Custom Android Gradle Template¶
- Change platform to Android (if not already done)
- Enable Custom Gradle Template: Project Settings → Player → Other Settings → Custom Gradle Template
- This will generate a file at
Plugins/Android/mainTemplate.gradle
Add play-services-auth Dependency¶
- Open the
.gradlefile located in your project atPlugins/Android/mainTemplate.gradle - Look for the
dependenciesblock - Add
play-services-authas shown below:
1 2 3 4 5 | |
Additional iOS Setup¶
When using Google Sign-In on Apple, the Login Flow depends on version 5 of Google's Sign-In SDK framework for iOS. Enabling sign-in on iOS also requires first-time setup regarding a custom URL scheme specific to your Google Cloud App, including overriding the openURL method on UnityAppController.
| Step | Detail |
|---|---|
| 1. Install the Google Sign-In SDK | Download Google Sign-In SDK version 5.0.0 or newer from https://developers.google.com/identity/sign-in/ios/sdk Create the Assets/Plugins/iOS folder in your Unity project if it does not already existExtract the SDK archive, then copy GoogleSignIn.framework and GoogleSignInDependencies.framework to your Assets/Plugins/iOS folder |
| 2. Add the URL scheme to your project | Your custom URL scheme is a reversed version of your iOS Client ID: com.googleusercontent.apps.<your-app-id>Unity → File → Build Settings → Player Settings → Other Settings Supported URL schemes: set size to 1 and enter your reversed ID as Element 0 |
3. Ensure app delegate handles openURL |
The app delegate for your app should handle application:openURL:options: and invoke the handleURL method of GIDSignIn. |
Method Swizzling for iOS URL Handling¶
The response from the Google Sign-In flow on Apple uses the custom Supported URL Scheme. This requires that the app delegate (UnityAppController) implement the openURL method. Using swizzling, we can define a new method and exchange implementations at runtime.
Place both GoogleSignInAppController.h and GoogleSignInAppController.mm within a Plugins/iOS folder in your Unity project.
GoogleSignInAppController.h
1 2 3 4 5 6 7 8 9 10 | |
GoogleSignInAppController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Google Sign-In Integration¶
The Beamable SDK contains a wrapper for native Google Sign-In behavior on Android and iOS. The provided class is called GoogleSignIn, which can be initialized with the correct Web Client ID, iOS Client ID, as well as a "target" (a context for the sign in callback to attach to), and a callback method name.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
After the Login process is started, the callback function (GoogleAuthResponse) will be invoked with a message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Handle Various Flow Scenarios¶
Now that we have the Google credential (token), we need to account for 3 different scenarios:
- New Player
- Returning Player already linked to Google
- Returning Player linking their account with Google
We can account for this by determining if we need to:
- Switch Player - Player wants to switch credentials to a new Player
- Create New Player - Player wants to create a new Player account
- Attach To Current Player - Player wants to Attach this 3rd Party Login to an already authenticated Player
Beamable SDK Initialization
The following assumes that you have initialized the Beamable SDK and it is stored in _beamContext variable.
1 2 | |
The following code will establish conditions for various flow scenarios.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Switch Users¶
In this example, the AuthService is used to authenticate a user with a third-party auth provider.
1 2 3 4 | |
Create New User¶
If we want to create a new user and apply the third-party credentials, we can use the following calls from AuthService.
1 2 3 4 5 6 7 | |
Link To Existing User¶
If the user already exists and is now trying to link their Google credentials to it, the flow is very similar to new user creation.
1 2 3 4 5 | |
Next Steps¶
- Players can edit account details (name, avatar).
- Players can switch accounts or sign in with various methods. See the Identity feature page for more info.