getPlayGamesCredential static method
Retrieves an OAuthCredential
for Play Games (Android).
This method interacts with the Play Games services to sign in the user and retrieve an
authentication code (serverAuthCode
). If successful, it generates a Firebase OAuth
credential from the auth code.
Throws:
- GameServicesFirebaseAuthException if the sign-in process fails or the user is not signed in.
Returns an OAuthCredential
that can be used with Firebase for user authentication.
Implementation
static Future<OAuthCredential> getPlayGamesCredential() async {
// Attempt to sign in with Play Games.
final bool success =
await GameServicesFirebaseAuth().signInWithGameService();
// If sign-in fails, throw an exception.
if (!success) {
throw GameServicesFirebaseAuthException(
code: GameServicesFirebaseExceptionCode.notSignedIntoGamesServices,
message:
'Failed to sign into Play Games. Please check your Play Games settings and try again.',
);
}
// Retrieve the authentication code from Play Games.
final String? authCode =
await GameServicesFirebaseAuth().getAndroidServerAuthCode();
// If the auth code is null, throw an exception.
if (authCode == null) {
throw GameServicesFirebaseAuthException(
code: GameServicesFirebaseExceptionCode.notSignedIntoGamesServices,
message:
'Failed to retrieve auth code from Play Games. Please check your Play Games settings and try again.',
);
}
// Return a Firebase OAuth credential using the retrieved serverAuthCode.
return PlayGamesAuthProvider.credential(serverAuthCode: authCode);
}