signInWithGoogle method

Future<UserCredential?> signInWithGoogle()

//////////// //////////// Signs in with Google

Implementation

// II.C - Social Authentication
///////////////
/// Signs in with Google
Future<UserCredential?> signInWithGoogle() async {
  try {
    // Begin interactive sign-in process
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();

    if (googleUser == null) {
      // User canceled the sign-in flow
      return null;
    }

    // Obtain auth details from request
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    // Check if tokens were obtained
    if (googleAuth.accessToken == null || googleAuth.idToken == null) {
      throw StateError('Failed to obtain authentication tokens from Google');
    }

    // Create new credential for Firebase
    final OAuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    // Sign in to Firebase with the Google credential
    return await _auth.signInWithCredential(credential);
  } on FirebaseAuthException catch (e) {
    switch (e.code) {
      case 'account-exists-with-different-credential':
        throw ValidationError('An account already exists with the same email address but different sign-in credentials', cause: e);
      case 'invalid-credential':
        throw ValidationError('The credentials have expired or are invalid', cause: e);
      case 'operation-not-allowed':
        throw StateError('Google sign in is not enabled for this project', cause: e);
      case 'user-disabled':
        throw StateError('This user account has been disabled', cause: e);
      default:
        throw RALIException('Failed to sign in with Google: ${e.message}', cause: e);
    }
  } catch (e) {
    // Handle other error types
    if (e is RALIException) {
      // Already a RALIException, just rethrow
      rethrow;
    }
    // Log the error for debugging
    logError(e, StackTrace.current, context: 'signInWithGoogle');
    throw RALIException('An unexpected error occurred during Google sign in', cause: e);
  }
}