Skip to content

[google_sign_in] Server auth code is always null on iOS after authenticate #175313

@amrgetment

Description

@amrgetment

When I request server code on google_sign_in for iOS I don't get it, and I debug the issue and found that I get access token instead of it
After several hours I found that the root cause is that I have to call the get server code first before authorize
so userId == null and the flow works

  Future<({String? accessToken, String? serverAuthCode})>
      _getAuthorizationTokens(AuthorizationRequestDetails request) async {
    String? userId = request.userId;

    // The Google Sign In SDK requires authentication before authorization, so
    // if the authentication isn't associated with an existing sign-in user
    // run the authentication flow first.
    if (userId == null) {
      SignInResult result = await _api.restorePreviousSignIn();

Here is my flow

    // I get server token first as _api.restorePreviousSignIn() in
    // google sign ios will return the server code only for non-logged in users
    // otherwise it will get the access token which we don't want
    final GoogleSignInServerAuthorization? googleServerAuthorization =
        await GoogleSignIn.instance.authorizationClient.authorizeServer(
          scopes,
        );

    if (googleServerAuthorization == null) {
      throw generateGoogleCancelledException();
    }
    if (googleServerAuthorization.serverAuthCode.isEmptyOrNull()) {
      throw Exception('Error signing in with Google');
    }

    final GoogleSignInAccount? googleSignInAccount = await GoogleSignIn.instance
        .attemptLightweightAuthentication(
          reportAllExceptions: true,
        );

I open the issue as a documentation issue as I am not sure if it is a missing documentation or a bug

Metadata

Metadata

Labels

P1High-priority issues at the top of the work listp: google_sign_inThe Google Sign-In pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions