the accessToken of the GoogleSignInAuthentication accessToken is null in the web flutter

Viewed 1621

I am trying to make the web version of my application available in flutter. In the mobile version, both on android and ios, login with google credentials works perfectly. In the web flutter I get the idToken correctly but the accessToken is null.

I followed the steps in this video: https://www.youtube.com/watch?v=0HLt1TYA600&list=LLe08PibNO0_PFjUVIu5WcxQ&index=3&t=1681s

Future<User> signInWithGoogle() async {
    final googleSignIn = GoogleSignIn();

    try {
      final GoogleSignInAccount googleSignInAccount =
          await googleSignIn.signIn();
      if (googleSignInAccount != null) {
        final GoogleSignInAuthentication googleSignInAuthentication =
            await googleSignInAccount.authentication;
        print(">>> accessToken");
        print(googleSignInAuthentication.accessToken);
        print(">>> idToken");
        print(googleSignInAuthentication.idToken);
        if (googleSignInAuthentication.accessToken != null &&
            googleSignInAuthentication.idToken != null) {
        final AuthCredential credential = GoogleAuthProvider.getCredential(
          idToken: googleSignInAuthentication.idToken,
          accessToken: googleSignInAuthentication.accessToken,
        );

        final AuthResult authResult =
            await FirebaseAuth.instance.signInWithCredential(credential);

        final FirebaseUser user = authResult.user;
        return _userFromFirebase(user);
        } else {
          throw PlatformException(
            code: 'ERROR_MISSING_GOOGLE_AUTH_TOKEN',
            message: 'Missing Google Auth Token',
          );
        }
      } else {
        throw PlatformException(
          code: 'ERROR_ABORTED_BY_USER',
          message: 'Sign in aborted by user',
        );
      }
    } catch (error) {
      return null;
    }
  }

1 Answers

I opened an issue on GitHub last night and got an answer:

Hey, please update to firebase_auth: ^0.18.0+1 - there was a bug with the assertion that was fixed. Latest versions can be seen here: https://firebase.flutter.dev/docs/migration/#2-update-firebase-plugins

To clarify, the problem isn't that that the accessToken is null, it's that it doesn't have to be non-null. The error is that there is an assert to check if either idToken or accessToken is not null, but really it checks that both are not null.

For completeness, here is the exact line change that fixes this bug. Notice how they changed from checking accessToken != null && idToken != null to accessToken != null || idToken != null.

Related