A month of Flutter: Sign in with Google

Building on the work configuring Firebase Auth for Android and iOS, it's time to go get a user's details. This will be leveraging the already installed google_sign_in and firebase_auth packages.

The big change is the addition of the Auth class.

class Auth {
  Auth({
    @required this.googleSignIn,
    @required this.firebaseAuth,
  });

  final GoogleSignIn googleSignIn;
  final FirebaseAuth firebaseAuth;

  Future<FirebaseUser> signInWithGoogle() async {
    final GoogleSignInAccount googleAccount = await googleSignIn.signIn();
    // TODO(abraham): Handle null googleAccount
    final GoogleSignInAuthentication googleAuth =
        await googleAccount.authentication;
    return firebaseAuth.signInWithGoogle(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
  }
}

This class gets passed a FirebaseAuth instance and a GoogleSignIn instance for interactions with the external authentication services. On GoogleSignIn the signIn method will trigger a user account selection prompt. When the user confirms their account selection a GoogleSignInAccount instance will be returned. On GoogleSignInAccount I'll call authentication to get the current user's tokens. Finally I'll exchange the GoogleSignInAuthentication for a FirebaseUser with signInWithGoogle.

I added a TODO to improve error handling so it doesn't get forgotten.

The app now has an authenticated FirebaseUser with all the accompanying details.

I'll now update SignInFab to leverage the new Auth service and print out the user's displayName in the Flutter logs.

void _handleSignIn() {
  auth
      .signInWithGoogle()
      .then((FirebaseUser user) => print('Hi ${user.displayName}'));
}
I/flutter (29142): Hi Birb Dev

SignInFab will also need to be passed an Auth instance in _MyHomePageState.

SignInFab(
  auth: Auth(
    firebaseAuth: FirebaseAuth.instance,
    googleSignIn: GoogleSignIn(),
  ),
)

This is what the account selector looks like on Android:

Sign in flow on Android

iOS prompts to allow access to google.com:

Sign in flow on iOS

Most of the new code is actually tests and mocking the external Google and Firebase APIs. It's complected and deserves it's own article so come back tomorrow to learn more.

Code changes

Posts in this series

  • A month of Flutter
  • A month of Flutter: create the app
  • A month of Flutter: configuring continuous integration
  • A month of Flutter: continuous linting
  • A month of Flutter: upgrading to 1.0
  • A month of Flutter: initial theme
  • A month of Flutter: no content widget
  • A month of Flutter: a list of posts
  • A month of Flutter: extract post item widget
  • A month of Flutter: post model and mock data
  • A month of Flutter: rendering a ListView with StreamBuilder
  • A month of Flutter: Stream transforms and failing tests
  • A month of Flutter: real faker data
  • A month of Flutter: rendering network images
  • A month of Flutter: FABulous authentication
  • A month of Flutter: configure Firebase Auth for Sign in with Google on Android
  • A month of Flutter: configure Firebase Auth for Sign in with Google on iOS
  • A month of Flutter: Sign in with Google
  • A month of Flutter: mocking Firebase Auth in tests
  • A month of Flutter: delicious welcome snackbar
  • A month of Flutter: navigate to user registration
  • A month of Flutter: user registration form
  • A month of Flutter: testing forms
  • A month of Flutter: setting up Firebase Firestore
  • A month of Flutter: awesome adaptive icons
  • A month of Flutter: set up Firestore rules tests
  • A month of Flutter: Firestore create user rules and tests
  • A month of Flutter: WIP save users to Firestore
  • A month of Flutter: user registration refactor with reactive scoped model
  • A month of Flutter: the real hero animation
  • A month of Flutter: a look back