signInWithGoogle method

Future<User?> signInWithGoogle()

Signs in a user with Google.

Returns the User object on success, or null on failure.

Implementation

Future<User?> signInWithGoogle() async {
  try {
    final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
    if (googleUser == null) {
      // User canceled the sign-in
      return null;
    }

    final GoogleSignInAuthentication googleAuth =
    await googleUser.authentication;

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

    UserCredential userCredential =
    await auth.signInWithCredential(credential);
    return userCredential.user;
  } on FirebaseAuthException catch (e) {
    // Log Google-specific authentication errors
    print('Error signing in with Google: ${e.message}');
    return null;
  } catch (e) {
    print('Unexpected error signing in with Google: $e');
    return null;
  }
}