React SDK refresh access token

Hi guys

I’m using auth0 react SDK (@auth0/auth0-react v1.2.0) and I need to refresh the access token when it expires.
Based on the docs I have to call getAccessTokenSilently() before every API request so the SDK will either retrieve it from the local cache or Auth0 server (if token has expired).

Since the SDK provides only hooks to call getAccessTokenSilently() I need to use it inside the components.
It would be great if I don’t have to repeat this in every component but to be able to put it inside the e.g. Axios interceptor and have it only at one place.

Is there any other option to solve this or I missed something from the docs?

Thank you
Ante

1 Like

Hi @antejadrija,

Welcome to the Community!

You may want to take a look at the gist which allows the app to access Auth0 client outside of the Auth0Provider wrapper: Using @auth0/auth0-spa-js outside of a React Component - #4 by klequis

Hi @stephanie.chamblee

This is useful.
The only downside is that I need to switch from auth0-react to auth0-spa-js.

auth0-react is awesome SDK and the only thing I’m missing is this.
If you can provide getAccessTokenSilently() outside of hooks it would make a huge difference. Otherwise we have to make our components dirty and copy paste the code in many places, or to switch to auth0-spa-js.

Thank you for your help.
Ante

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.