getGameCenterCredential static method

Future<OAuthCredential> getGameCenterCredential()

Retrieves an OAuthCredential for Game Center (iOS).

This method checks if the user is already signed into Game Center. If the user is not signed in, it attempts to sign them in and generate an OAuth token.

Throws:

Returns an OAuthCredential that can be used with Firebase to authenticate the user.

Implementation

static Future<OAuthCredential> getGameCenterCredential() async {
  // Check if the user is already signed into Game Center.
  final bool alreadySignedIn =
      await GameServicesFirebaseAuth().isAlreadySignInWithGameService();

  if (alreadySignedIn) {
    // Return the Game Center OAuth credential if already signed in.
    return GameCenterAuthProvider.credential();
  }

  // Attempt to sign in with Game Center.
  final bool signInSuccess =
      await GameServicesFirebaseAuth().signInWithGameService();

  // If sign-in fails, throw an exception.
  if (!signInSuccess) {
    throw GameServicesFirebaseAuthException(
      code: GameServicesFirebaseExceptionCode.notSignedIntoGamesServices,
      message:
          'Failed to sign into Game Center. Please check your Game Center settings and try again.',
    );
  }

  // Return the Game Center credential for Firebase after successful sign-in.
  return GameCenterAuthProvider.credential();
}