Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleSignIn.getTokens() not returning new tokens in android #926

Open
Infinity-Intellect opened this issue Oct 29, 2020 · 15 comments
Open

Comments

@Infinity-Intellect
Copy link

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
@devksingh4
Copy link

I am also experiencing this issue

@sammysium
Copy link

@devksingh4 @Infinity-Intellect did you guys find a solution for this?

@devksingh4
Copy link

@sammysium I unfortunately did not find a solution to this. Are many other people also experiencing this issue?

@MilosR
Copy link

MilosR commented Jan 2, 2021

I think you need to call signInSilently()

@sammysium
Copy link

sammysium commented Jan 3, 2021 via email

@MilosR
Copy link

MilosR commented Jan 3, 2021

I am not completely sure about this, but I think solution should be to check whether JWT expired, if it is then just call signInSilently() and after you will get the new token using getTokens(). Do not call signOut()

@devksingh4
Copy link

I am not completely sure about this, but I think solution should be to check whether JWT expired, if it is then just call signInSilently() and after you will get the new token using getTokens(). Do not call signOut()

That is what I have been doing, and it is still returning the expired tokens.

@VEmpink
Copy link

VEmpink commented Jan 18, 2021

Have you tried a forceCodeForRefreshToken property? Like:

GoogleSignin.configure({
  forceCodeForRefreshToken: true,
});

@sammysium
Copy link

sammysium commented Jan 18, 2021 via email

@VEmpink
Copy link

VEmpink commented Jan 18, 2021

Yes..it is set to true.

On Mon, Jan 18, 2021, 6:41 AM Firmansyah @.***> wrote: Have you tried a forceCodeForRefreshToken property? Like: GoogleSignin.configure({ forceCodeForRefreshToken: true,}); — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#926 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALF7AKHHGF6W3Q4Y756HAELS2O3YFANCNFSM4TDXYANA .

May I see your configuration?

@devksingh4
Copy link

So upon further investigation, it looks like that GoogleSignin.getCurrentUser() will not always return null when no user is signed in. I tested this by signing all Google users out of the device and then calling GoogleSignin.getCurrentUser() - it did not return null, but instead returned the old user's data, presumably stored from before. My current workaround is to just query my backend API server to see if it can use the given ID token - if it can't, then user sign in needs to be forced again. It looks like this library employs some caching beyond what is native?

@annuh
Copy link

annuh commented Aug 26, 2023

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: #792 (comment))

@awais882s
Copy link

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

@castelvani
Copy link

any solutions?

@kesha-antonov
Copy link

kesha-antonov commented Jan 17, 2024

This is what worked for me:

loginWithGoogle = async () => {
  const { GOOGLE } = SOCIAL_KINDS

  const signOutGoogle = async accessToken => {
    if (accessToken)
      await GoogleSignin.clearCachedAccessToken(accessToken)
    await GoogleSignin.signOut()
  }

  // INSTALL PLAY SERVICES IF NEEDED
  try {
    await GoogleSignin.hasPlayServices()
  } catch (e) {
    logException('loginWithGoogle-1 e', e)
  }

  // CLEANUP TOKEN IF WAS SIGNED IN
  try {
    const isSignedIn = await GoogleSignin.isSignedIn()
    if (isSignedIn) {
      const currentUser = await GoogleSignin.getCurrentUser()
      if (!currentUser)
        await GoogleSignin.signInSilently()

      const resp = await GoogleSignin.getTokens()
      await signOutGoogle(resp.accessToken)
    }
  } catch (e) {
    logException('loginWithGoogle-2 e', e)
  }

  let accessToken

  try {
    const { idToken, user: { name: fullName } } = await GoogleSignin.signIn()
    const resp = await GoogleSignin.getTokens()
    accessToken = resp.accessToken

    ...
  } catch (e) {
    logException('loginWithGoogle-3 e', e)

    ...
  } finally {
    await signOutGoogle(accessToken)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants