Authenticate Using Google Play Games Services on Android

You can use Google Play Games services to sign players in to an Android game
built on Firebase. To use Google Play Games services sign-in with Firebase,
first sign the player in with Google Play Games, and request an OAuth 2.0 auth
code when you do so. Then, pass the auth code to PlayGamesAuthProvider to
generate a Firebase credential, which you can use to authenticate with Firebase.

Link your Firebase project to your Play publisher account

Click Add new game. In the new game dialog, click I already use Google
APIs in my game and click the name of your Firebase project in the list.
Select a game category, then click Continue to go to the Game Details
page.

At the end of the Game Details page, ensure that all required APIs are
enabled.

Then, open the Linked apps page and click Android. Specify your
game's package name, and click Save and continue. The console will
display your Android client ID. You can ignore this value.

On the Testing page, whitelist the email addresses of any users who need
to be able to sign in to your game before you release it on the Play store.

KotlinAndroid

If the player isn't signed in, present the player with your game's
signed-out experience, including the option to sign in.

After a player signs in with Play Games either silently or interactively,
get the auth code from the GoogleSignInAccount object, exchange it for a
Firebase credential, and authenticate with Firebase using the Firebase
credential:

If the call to signInWithCredential succeeds you can use the getCurrentUser method to get the user's account data.

Next steps

After a user signs in for the first time, a new user account is created and
linked to their Play Games ID. 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.

In your game, you can get the user's Firebase UID from the FirebaseUser
object:

JavaAndroid

FirebaseUser user = mAuth.getCurrentUser();
String playerName = user.getDisplayName();
// 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 FirebaseUser.getIdToken() instead.
String uid = user.getUid();