React-google-login: Sometimes getAuthResponse returned access_token to undefined.

Created on 24 Apr 2020  Â·  7Comments  Â·  Source: anthonyjgrove/react-google-login

hi.

Sometimes access_token is not defined after logging in.

So I debugged and found that function getAuthResponse, which is used in the library, was returning undefined.

The official document states that access_token is not returned when:

By default, the access token and requested scopes are not returned when fetched_basic_profile is true (the default value) and no additional scopes are required.

I have to specify fetched_basic_profile as true, and scope as profile email.
The default value for this library is the same as the value I should use, so I didn’t specify it in the code.

I am using the login component as below.

  <GoogleLogin
      render={LoginButton}
      clientId={googleClientId}
      onRequest={onGoogleLoginRequest}
      onSuccess={onGoogleLoginSuccess}
      onFailure={onGoogleLoginFailure}
      cookiePolicy=“single_host_origin”
    />

To solve this problem, I would like to modify the parameter of function getAuthResponse.

Or is there another better solution?

Most helpful comment

@Sultanyaron As the README and GoogleUser object said, You should add true as
a parameter to getAuthResponse().

// You can always get the `access_token` in this way
res.getAuthResponse(true).access_token;  

All 7 comments

Edit

Now the tc field in res somehow has been changed to wc. According to https://github.com/anthonyjgrove/react-google-login/issues/17, this property name is unreliable.


I notice that there is a field tc always contains access_token when the access_token is undefined. I use it as an alternative.

Im getting accessToken: undefined in the main object and another object inside called wc which contains access_token.

also tried using getAuthResponse and it did not return an access token

Did anyone find a solution for that?

@Sultanyaron As the README and GoogleUser object said, You should add true as
a parameter to getAuthResponse().

// You can always get the `access_token` in this way
res.getAuthResponse(true).access_token;  

getAuthResponse doesn't accept any arguments as per https://github.com/anthonyjgrove/react-google-login/blob/master/index.d.ts#L30

While calling res.getAuthResponse().access_token returns undefined for me

@Sultanyaron As the README and GoogleUser object said, You should add true as
a parameter to getAuthResponse().

// You can always get the `access_token` in this way
res.getAuthResponse(true).access_token;  

Can confirm that this properly sets the access_token. @anthonyjgrove should we open a PR for this? At least set it to true when access_type is token

@phanshiwen
It is working properly and i am getting the token with the following:

       //@ts-ignore
    const googleAccessToken = loginResponse.getAuthResponse(true).access_token;

According to the definition file getAuthResponse don't accept args, this is something that need to get fixed.

Edit

Now the tc field in res somehow has been changed to wc. According to #17, this property name is unreliable.

I notice that there is a field tc always contains access_token when the access_token is undefined. I use it as an alternative.

Now the wc has changed to xc

Is this due to a change in Google's response?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ethan0007 picture Ethan0007  Â·  3Comments

MiLeung picture MiLeung  Â·  4Comments

pogran picture pogran  Â·  4Comments

scolyer picture scolyer  Â·  3Comments

SSR
rlancer picture rlancer  Â·  8Comments