google-signin: GoogleSignIn.getTokens() not returning new tokens in android

Once my token expires, I first call GoogleSignIn.clearCachedAccessToken(accessToken) and then call GoogleSignIn.getTokens(). However, I do not get a set of fresh tokens, rather I am provided with the same expired set of tokens.

Steps to Reproduce

Below is my refresh code function :-

export function refreshExpiredToken(exp,accessToken,dispatch){
    return new Promise(async (resolve)=>{
        if(moment().valueOf() > exp){
            await GoogleSignin.clearCachedAccessToken(accessToken)
            const tokens = await GoogleSignin.getTokens()
            console.log(tokens)
        }
        resolve(true)
    })    
}

Expected Behavior

I expected to receive a new set of tokens, that has an expiry time 1 hour from the time getTokens() is called.

Actual Behavior

I receive the old set of tokens, that have already expired.

Environment

@react-native-community/google-signin: ^4.0.3

System:
    OS: Windows 10 10.0.18363
  Languages:
    Java: javac 11
    Python: 3.8.5
  npmPackages:
    react: 16.11.0 => 16.11.0
    react-native: 0.62.2 => 0.62.2

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17

Most upvoted comments

FYI, I was having the same issue, and fixed by using the following code:

import { GoogleSignin, statusCodes } from "@react-native-google-signin/google-signin";

// Seems that without `signInSilently()`, `getCurrentUser()` sometimes return `null` (?)
try {
    await GoogleSignin.signInSilently();
} catch (error) {
    if (error.code !== statusCodes.SIGN_IN_REQUIRED) {
        throw error;
    }
}

// Remove cached access_token, since `GoogleSignin.getTokens()` may return expired tokens
const oldUser = await GoogleSignin.getCurrentUser();
if (oldUser) {
    const oldTokens = await GoogleSignin.getTokens();
    if (oldTokens.accessToken) {
        console.log('Removing old access token', {oldTokens});
        await GoogleSignin.clearCachedAccessToken(oldTokens.accessToken);
    }
}
await GoogleSignin.signOut(); // https://github.com/react-native-google-signin/google-signin/issues/1022#issuecomment-1115847835

(credits: https://github.com/react-native-google-signin/google-signin/issues/792#issuecomment-816821710)

Still looks to be an ongoing issue

Please help me I get access from a google user but I need a refresh token I can get please give me proper any docs or solutions in react native