logInWithGoogle method

Future<AuthUser?> logInWithGoogle()

Starts the Sign In with Google Flow.

Throws a LogInWithGoogleFailure if an exception occurs.

Implementation

Future<AuthUser?> logInWithGoogle() async {
  try {
    late final AuthCredential credential;
    if (isWeb) {
      final googleProvider = GoogleAuthProvider();

      final credential = await _firebaseAuth.signInWithPopup(
        googleProvider,
      );
      if (credential.credential != null) {
        final userCredentials =
            await _firebaseAuth.signInWithCredential(credential.credential!);
        return userCredentials.user == null
            ? AuthUser.empty
            : userCredentials.user!.toUser;
      }
    } else {
      final googleUser = await _googleSignIn.signIn();

      if (googleUser == null) return null;

      final googleAuth = await googleUser.authentication;
      credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );
      final userCredentials =
          await _firebaseAuth.signInWithCredential(credential);
      return userCredentials.user == null
          ? AuthUser.empty
          : userCredentials.user!.toUser;
    }

    return null;
  } on FirebaseAuthException catch (e) {
    if (e.code != 'popup-closed-by-user') {
      throw LogInWithGoogleFailure.fromCode(e.code);
    }
    return null;
  } catch (e, s) {
    print(e);
    print(s);
    throw const LogInWithGoogleFailure();
  }
}