Authenticate Using Game Center

You can use Game Center to sign players in to an iOS game built on Firebase. To
use Game Center sign-in with Firebase, first make sure the local player is
signed in with Game Center, and then use the GameCenterAuthProvider object to
generate a Firebase credential, which you can use to authenticate with Firebase.

Integrate Game Center sign-in into your game

Be sure the bundle ID you provide to iTunes Connect matches the bundle ID you
used when you connected your app to your Firebase project.

As part of your Game Center integration, you define an authentication handler
that is called at multiple points in the Game Center authentication process. In
this handler, check if the player is signed in with Game Center. If so, you can
continue to sign in to Firebase.

Authenticate with Firebase

After you determine that the local player has signed in with Game Center,
sign the player in to your game by creating an AuthCredential object with
GameCenterAuthProvider.getCredential() and passing that object to
signIn(with:):

Swift

// Get Firebase credentials from the player's Game Center credentials
GameCenterAuthProvider.getCredential() { (credential, error) in
if let error = error {
return
}
// The credential can be used to sign in, or re-auth, or link or unlink.
Auth.auth().signIn(with:credential) { (user, error) in
if let error = error {
return
}
// Player is signed in!
}

Objective-C

// Get Firebase credentials from the player's Game Center credentials
[FIRGameCenterAuthProvider getCredentialWithCompletion:^(FIRAuthCredential *credential,
NSError *error) {
// The credential can be used to sign in, or re-auth, or link or unlink.
if (error == nil) {
[[FIRAuth auth] signInWithCredential:credential
completion:^(FIRUser *user, NSError *error) {
// If error is nil, player is signed in.
}];
}
}];

Next steps

After a user signs in for the first time, a new user account is created and
linked to their Game Center 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 User object: