InquisitiveTom
InquisitiveTom

Reputation: 483

Firebase Auth Refresh Token?

How do I get access to the refreshed token in Firebase Auth?

I'm building a React app, and on the signin button click run Login function. This function gives me a Google API token which I use to access google drive api.

The problem is that is token expires after an hour. This post mentions:

"If you need to know when the SDK refreshes the token in order to get a new one immediately, you should instead use onIdTokenChanged to set up a callback that will be invoked every time the user's token changes."

As such I've gone ahead and set this function up. However, how do I access this new updated token so that I can pass it along to the rest of the app?

Login Function

const login = async () => {
    signInWithPopup(auth, provider)
      .then((result) => {
        // This gives you a Google Access Token. You can use it to access the Google API.
        const credential = GoogleAuthProvider.credentialFromResult(result);
        const token = credential.accessToken;
}

onIdTokenChanged

 onIdTokenChanged(auth, (currentUser) => {
    console.log(currentUser);
    setUser(currentUser);
  });

Upvotes: 1

Views: 209

Answers (0)

Related Questions