How to access access token, expiration, etc. in React app?

In Auth0 React what is the best approach for the following design goals:

  • Be able to get/use the access token in any React component to make API calls
  • Determine when the access token is about to expire and use the refresh token to get a new access token
  • Use the access token expiration time to warn the user in advance of the token expiration.

What is unclear in the Quick Start is whether getAccessTokenSilently() is making a call to Auth0 every time an access token is requested or is it reading from cacheLocation and only calling Auth0 when the access token has expired?

If the cacheLocation is memory, is there a way to get the information? If localStorage the data structure would provide all the information for our design goals.

Hi @MHC-Pels,

Welcome to the Community!

Checkout the react quickstart:

This should all be handled by the SDK, but if you want to dig down, the response from the token endpoint includes an expires_in value, which is the token’s expiration.

Why would you warn the user? They shouldn’t need to know when the access token expires, this could be very often.

The latter is happening, it is using the token from the cache first, if there is no token, if it does not include the requested info, or if it is expired, a new one is requested.

You can choose the cache location.

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