signInWithGoogle method

Future<UserCredential> signInWithGoogle({
  1. List<String> scopes = const <String>['email', 'profile'],
})

Implementation

Future<UserCredential> signInWithGoogle({
  List<String> scopes = const <String>[
    'email',
    'profile',
  ],
}) async {
  GoogleSignInAccount? googleUser;
  try {
    googleUser = await _googleSignIn.attemptLightweightAuthentication(
      reportAllExceptions: true,
    );
  } on GoogleSignInException catch (e) {
    if (!_isGoogleUserCancellation(e)) {
      rethrow;
    }
  }

  googleUser ??= await _authenticateWithGoogle(scopes);

  GoogleSignInClientAuthorization? authorization;
  if (scopes.isNotEmpty) {
    try {
      authorization =
          await googleUser.authorizationClient.authorizeScopes(scopes);
    } on GoogleSignInException catch (e) {
      if (_isGoogleUserCancellation(e)) {
        throw const AuthCancelledException('Google permissions not granted');
      }
      rethrow;
    }
  }

  final googleAuth = googleUser.authentication;
  if (googleAuth.idToken == null) {
    throw FirebaseAuthException(
      code: 'missing-google-id-token',
      message: 'Google sign-in returned no idToken.',
    );
  }

  final credential = GoogleAuthProvider.credential(
    idToken: googleAuth.idToken,
    accessToken: authorization?.accessToken,
  );

  final userCred = await _auth.signInWithCredential(credential);
  await getFirebaseIdToken();
  return userCred;
}