anthonyjgrove / react-google-login

A React Google Login Component
https://anthonyjgrove.github.io/react-google-login
MIT License
1.84k stars 427 forks source link

Sometimes getAuthResponse returned access_token to undefined. #322

Open zoripong opened 4 years ago

zoripong commented 4 years ago

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?

NevenLiang commented 4 years ago

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.

Sultanyaron commented 4 years ago

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?

NevenLiang commented 4 years ago

@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;  
maksimf commented 4 years ago

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

phanshiwen commented 4 years ago

@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

Sultanyaron commented 4 years ago

@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.

smarajitWOW commented 3 years ago

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?