Firebase Auth Games Services

Enables logging into Firebase using Android Play Games and iOS Game Center.

Utilize this plugin for low-friction sign in for Flutter apps. No emails, no passwords, no pop-ups.

Glossary

  • Games Services: Collective name for Play Games and Game Center.

Dependencies

This plugin depends on:

Please ensure you have these installed before you begin.

Installation

Please follow these steps carefully as there are multiple pitfalls that might lead to cryptic error messages. These instructions were taken from official Google/Apple documentation and are subject to change. You can access the original documentation by clicking the * at each step.

Android configuration

  1. Ensure you have added Android and iOS apps to your Firebase project. You can do so via Firebase console.

    Firebase apps
  2. * Set the SHA-1 Fingerprint of your Android app in Firebase console.

  3. * Enable Google Play Games as a sign-in provider in Firebase console. You can find the OAuth2 Client ID and secret at the Credentials section of GCC under the name "Web client (auto created by Google Service)".

  4. * * Setup Play Games in the Google Play Console. Follow the on-screen prompts. When you arrive at the Credentials stage make sure to add 2 credentials: (1) Game server type credential where you use the client ID from the step above; (2) Android type credential where you use the Android credential autogenerated by Firebase.

    Play Games credentials
  5. * Configure your OAuth consent screen by following the original directions from Google (<- click the star).

  6. Again in the Google Play Console add your Gmail to two distinct tester lists: one in Testing > Internal testing and the other in Play Games Services > Setup and management > Testers.

  7. * Create a android/app/src/main/res/values/strings.xml file, if one doesn't exist. To this file add a string resource reference and set your project ID as the value. In Google Play Console, you can find your project ID under your game name in the Configuration page. For example:

    <!-- res/values/strings.xml -->
    <resources>
        <!-- Replace 0000000000 with your game’s project id. Example value shown below. -->
        <string translatable="false"  name="game_services_project_id"> 0000000000 </string>
    </resources>
    
  8. * To the same file add your OAuth2 Client ID. You can find this at the Credentials section of GCC under the name "Web client (auto created by Google Service)". For example:

    <!-- res/values/strings.xml -->
    <resources>
        <!-- Replace 0000000000 with your game’s project id. Example value shown below. -->
        <string translatable="false"  name="game_services_project_id"> 0000000000 </string>
        <!-- Replace with your OAuth2 Client ID from Google Cloud Console. -->
        <string translatable="false" name="firebase_auth_oauth_2_web_client_id">XXXXXXXX.apps.googleusercontent.com</string>
    </resources>
    
  9. * In your android/app/src/main/AndroidManifest.xml file add the following within the <application> element:

    <!-- Required by Play Games SDK to specify application ID. -->
    <meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/game_services_project_id" />
    
    <!-- Required by firebase_auth_games_services plugin. -->
    <meta-data
        android:name="com.chunkytofustudios.firebase_auth_games_services.OAUTH_2_WEB_CLIENT_ID"
        android:value="@string/firebase_auth_oauth_2_web_client_id" />
    

iOS configuration

  1. * Open your project in XCode. In the Signing & Capabilities tab add the "Game Center" capability.

  2. * Ensure that the bundle ID in Firebase console matches the one listed in XCode.

Update Firebase config

If you made any changes via Firebase Console or Google Cloud Console run the following command to update your local files:

flutterfire configure

Usage

First, import the plugin:

import 'package:firebase_auth_games_services/firebase_auth_games_services.dart';

You can now ask the user to sign-in via Games Services with a single command:

await FirebaseAuth.instance.signInWithGamesServices();

Or if the user is already signed into Firebase you can check if their Firebase account is linked to Games Services via:

final user = FirebaseAuth.instance.currentUser;
// Ensure user is signed in (not null and not anonymous).
bool isAlreadyLinked = user.isLinkedWithGamesServices();

If this returns false you can request to link the accounts with:

final user = FirebaseAuth.instance.currentUser;
// Ensure user is signed in (not null and not anonymous).
await _user?.linkWithGamesServices();

Both commands support a silent: true argument which will not bother the user (no sign-in UI will appear) if they are not already signed into Games Services at the OS level.

Finally, please see the code reference to see the types of exceptions these methods might throw & ensure to catch them.

Conribute

This is a very simple plugin with about ~200 lines of business logic.

The plugin is open source and available at GitHub.

Please feel free to file bugs and submit PRs.

About

Developed by @orkun1675 and used actively in Chunky Tofu Studios projects.