question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refresh the google auth manually through code

See original GitHub issue

I am having an issue with finding out how to refresh the Google auth through code. The main problem is that after the 1 hour session has timed out then I start to get errors.

I have set isSignedIn to true, but this doesn’t really help me since I am on a dashboard type of page and want it to refresh when an api call fails (using axios interceptor to do this). I would love to just try a refresh on the google auth when there is a 401 returned. I am already successfully doing this with my API and a 1 hour JWT, so it seems reasonable to use the same method for the google auth refresh. This kind of works, but according to other comments on the internet I should just do gapi.something.refreshToken basically, but this doesn’t work with me because the gapi is not available when I try to call it (probably due to the 401 errors causing the rest of the script not to load or something)

I was hoping this plugin would just kind of handle that for me, but it seems not. Any ideas I could try?

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
gigcasterscommented, Jul 22, 2021
export const refreshTokenSetup = (result) => {
    let refreshTiming = (result.tokenObj.expires_in || 3600 - 5 * 60) * 1000;
    
    const refreshToken = async () => {
        const newAuthRes = await result.reloadAuthResponse();
        refreshTiming = (newAuthRes.expires_in || 3600 - 5 * 60) * 1000;
        // Save new Token --> newAuthRes.access_token
        console.log(`New Authentication Result: ${newAuthRes}`);
        console.log(`New Authentication Token: ${newAuthRes.idToken}`);
        setTimeout(refreshToken, refreshTiming);
    }
    setTimeout(refreshToken, refreshTiming);
}
0reactions
nickyoung87commented, Aug 3, 2021

Yeah so I think I just got this working. I basically set up an axios interceptor to run and then call the custom hook from the interceptor once the gapi object is loaded back in.

I still think this is something the the package should make a lot easier to handle, but it seems my issue is solved for now.

Thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using OAuth 2.0 to Access Google APIs | Authorization
1. Obtain OAuth 2.0 credentials from the Google API Console. · 2. Obtain an access token from the Google Authorization Server. · 3....
Read more >
Get verification codes with Google Authenticator - Android
Get verification codes with Google Authenticator. If you set up 2-Step Verification, you can use the Google Authenticator app to receive codes.
Read more >
get google oauth refresh token after first login - Stack Overflow
I found the solution. The refresh token is only sent with the response when the consent screen is shown to the user.
Read more >
Using the Google Authenticator app with Multifactor ...
If you use counter-based codes, you will need to press the refresh button next to the code in the Google Authenticator App each...
Read more >
Google API - Get AccessToken and Refresh Token Part-1
Get access_token and refresh_token curl \ --request POST \ --data " code =[Authentcation code from authorization link]&client_id=[Application ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found