Search code examples
reactjsfirebasereact-nativegoogle-signin

how to check google sing in token is expired or not


I'm facing an issue with the expiration of Google Sign-In tokens in my React Native app. Despite my attempts to resolve it, I haven't been successful. I understand that obtaining a refresh token is the appropriate solution, but I'm unsure how to implement it.

I've tried logging the user out when the token expires, but I know this isn't the correct approach. Instead, I want to obtain a refresh token to automatically refresh the access token without requiring the user to sign in again.

Could someone please guide me on how to obtain and utilize a refresh token in React Native for Google Sign-In? I've searched extensively but haven't found a clear solution. Any help would be greatly appreciated.


Solution

  • I got a solution finally

     const getTokens = async () => {
      const data = await AsyncStorage.getItem("AccessToken");
    
      const parts = data
        .split(".")
        .map((part) =>
          Buffer.from(
            part.replace(/-/g, "+").replace(/_/g, "/"),
            "base64"
          ).toString()
        );
    
      const payload = JSON.parse(parts[1]);
    
      console.log(Math.floor(Date.now() / 1000), "JWT payload", payload?.exp);
      Math.floor(Date.now() / 1000) >= payload?.exp
        ? refreshGoogleToken()
        : console.log("your token is up to date");
    };
    

    I decode the token and get exp which gives me token expired time stamp and if token is expired then I refresh token

    npm i buffer
    

    you can use above buffer library for decode token