signInWithGoogle method

Future<UserCredential> signInWithGoogle()

Signs in the user using Google authentication and returns the user credential.

Implementation

Future<UserCredential> signInWithGoogle() async {
  try {
    // Sign out current user if not anonymous
    if (_auth.currentUser != null && _auth.currentUser!.isAnonymous != true) {
      await _auth.signOut();
    }

    if (_auth.currentUser != null && _auth.currentUser!.isAnonymous == true) {
      // Trigger the authentication flow
      final googleUser = await _googleSignIn.signIn();
      // Obtain the auth details from the request
      final GoogleSignInAuthentication? googleAuth =
          await googleUser?.authentication;
      if (googleAuth is GoogleSignInAuthentication &&
          (googleAuth.accessToken is String ||
              googleAuth.idToken is String)) {
        // Create a new credential
        final googleCredential = GoogleAuthProvider.credential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );

        final credential =
            await _auth.currentUser!.linkWithCredential(googleCredential);

        await _auth.currentUser!.reload();
        if (_auth.currentUser!.email is String &&
            _auth.currentUser!.email!.isNotEmpty) {
          await resendEmailVerification(_auth.currentUser!.email!);
        }
        return credential;
      } else {
        throw AuthDataServiceException(
          message: 'Google Sign In Failed',
        );
      }
    }

    // Trigger the authentication flow
    final googleUser = await _googleSignIn.signIn();
    // Obtain the auth details from the request
    final GoogleSignInAuthentication? googleAuth =
        await googleUser?.authentication;
    if (googleAuth is GoogleSignInAuthentication &&
        (googleAuth.accessToken is String || googleAuth.idToken is String)) {
      // Create a new credential
      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );
      return await _auth.signInWithCredential(credential);
    } else {
      throw AuthDataServiceException(
        message: 'Google Sign In Failed',
      );
    }
  } catch (err) {
    if (err is FirebaseAuthException) {
      throw AuthDataServiceException.fromRdevException(err.toRdevException());
    }
    if (err is AuthDataServiceException) {
      rethrow;
    }
    throw AuthDataServiceException(
      message: err.toString(),
    );
  }
}