Flutter: Google Play Games Sign-in with Firebase

Introduction

This post shares the mistakes I made during setting up Google Play Games for Firebase. Firebase is so amazing that it integrates many sign-in methods; however, the setup of a certain sign-in method might be complex and error-prone with unhelpful error messages at the same time.

Environment

  • Flutter 2.0.6
  • Dart 2.12.3
  • firebase_core 1.1.0
  • firebase_auth 1.1.2
  • google_sign_in 5.0.2

Documents

First, follow all the related official documents carefully.

FlutterFire (Firebase Flutter SDK)

Firebase

Google Play Games Services

Common Pitfalls

Debug signing certificate SHA-1 not added to Firebase

When debugging your app, it’s automatically signed with a debug certificate generated by Android SDK, which is different with the release certificate and not recognized by Firebase. The debug keystore is in ~/.android/debug.keystore with alias androiddebugkey and password android.

To get the debug certificate, the most convenient way is using gradlew.

# your_project/android/
$ ./gradlew signingReport

Note that you must change directory to android. The following example would fail.

# your_project/
$ ./android/gradlew signingReport

FAILURE: Build failed with an exception.

* What went wrong:
Task 'signingReport' not found in root project 'your_project'.

* Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

Or, you can use keytool to extract certificate from debug.keystore.

# Windows
$ keytool -list -v -keystore "%USERPROFILE%/.android/debug.keystore" -alias androiddebugkey -storepass android -keypass android
# Linux
$ keytool -list -v -keystore "~/.android/debug.keystore" -alias androiddebugkey -storepass android -keypass android

After you get the debug certificate, add the SHA1 certificate to Project Settings > General in Firebase.

Google sign-in method not enabled in Firebase

Even we only want to support Google Play Games Services, Google sign-in method must be enabled in Firebase as well.

Otherwise, there would be an exception.

I/flutter ( 3888): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: [firebase_auth/operation-not-allowed] The given sign-in provider is disabled for this Firebase project. Enable it in the Firebase console, under the sign-in method tab of the Auth section. [ The identity provider configuration is disabled. ]

email is required in GoogleSignIn’s Scopes

Even we don’t need to access extra Google APIs, email scope is required for authentication.

GoogleSignIn googleSignIn = GoogleSignIn(
  signInOption: SignInOption.games,
  scopes: ['email'],
);

Or there would be an exception without helpful message.

Null check operator used on a null value

Example

static Future signIn() async {
  final AuthCredential googleCredential = await getGoogleCredential();
  if (googleCredential != null) {
    await FirebaseAuth.instance.signInWithCredential(googleCredential);
  } else {
    await FirebaseAuth.instance.signInAnonymously();
  }
}

static Future<AuthCredential> getGoogleCredential() async {
  final GoogleSignIn googleSignIn = GoogleSignIn(
    signInOption: SignInOption.games,
    scopes: ['email'],
  );

  final GoogleSignInAccount googleUser = await googleSignIn.signIn().catchError((error) {
    print('Failed to sign in with Google Play Games: $error');
  });

  if (googleUser == null) {
    print('Failed to sign in with Google Play Games.');
    return Future.value(null);
  }

  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  return credential;
}

Leave a Comment