signInWithGoogle method

  1. @override
Future<Response<Credential>> signInWithGoogle()
override

Implementation

@override
Future<Response<Credential>> signInWithGoogle() async {
  const response = Response<Credential>();
  try {
    GoogleSignInAccount? result;
    final auth = GoogleSignIn(scopes: ['email']);
    final isSignedIn = await auth.isSignedIn();
    if (isSignedIn) {
      result = await auth.signInSilently();
    } else {
      result = await auth.signIn();
    }
    if (result != null) {
      final authentication = await result.authentication;
      final idToken = authentication.idToken;
      final accessToken = authentication.accessToken;
      if (accessToken != null && idToken != null) {
        final credential = GoogleAuthProvider.credential(
            idToken: idToken, accessToken: accessToken);
        final receivedData = auth.currentUser;
        final data = Credential(
          id: receivedData?.id,
          email: receivedData?.email,
          name: receivedData?.displayName,
          photo: receivedData?.photoUrl,
        );
        return response.copy(
          data: data.copyWith(
            accessToken: accessToken,
            idToken: idToken,
            credential: credential,
          ),
        );
      } else {
        return response.copy(error: 'Token not valid!');
      }
    } else {
      return response.copy(error: 'Sign in failed!');
    }
  } on FirebaseAuthException catch (e) {
    return response.copy(error: e.message);
  }
}