React: Token Renewal

Gravatar for
By Andres Aguiar

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • React 15.5
Show requirements

For security, keep the expiry time of a user's Access Token short.

When you create an API in the Auth0 dashboard, the default expiry time for browser flows is 7200 seconds (2 hours).

This short expiry time is good for security, but can affect user experience. To improve user experience, provide a way for your users to automatically get a new Access Token and keep their client-side session alive. You can do this with Silent Authentication.

You can control the expiry time of an Access Token from the APIs section. You can control the expiry time of an ID Token from the Applications section. These settings are independent.

Add Token Renewal

To the Auth service, add a method which calls the checkSession method from auth0.js. If the renewal is successful, use the existing setSession method to set new tokens in local storage.

// src/Auth/Auth.js

renewToken() {
  this.auth0.checkSession({}, (err, result) => {
      if (err) {
      } else {

The Access Token should be renewed when it expires. In this tutorial, the expiry time of the token is stored in local storage as expires_at.

Define a timing mechanism for renewing the token.

You can define any timing mechanism you want. You can choose any library that handles timers. This example shows how to use a setTimeout call.

In the Auth service, add a property called tokenRenewalTimeout which refers to the setTimeout call.

Add a method called scheduleRenewal to set up the time when the authentication is silently renewed. The method subtracts the current time from the Access Token's expiry time and calculates delay. The setTimeout call uses the calculated delay and makes a call to renewToken.

The setTimeout call call is assigned to the tokenRenewalTimeout property. When the user logs out, the timeout is cleared.

// src/Auth/Auth.js

// ...
scheduleRenewal() {
  const expiresAt = JSON.parse(localStorage.getItem('expires_at'));
  const delay = expiresAt -;
  if (delay > 0) {
    this.tokenRenewalTimeout = setTimeout(() => {
    }, delay);

You can now include a call to the scheduleRenewal method in the setSession method.

// src/Auth/Auth.js

// ...
setSession(authResult) {
  // Set the time that the Access Token will expire at
  let expiresAt = JSON.stringify(
    authResult.expiresIn * 1000 + new Date().getTime()

  localStorage.setItem('access_token', authResult.accessToken);
  localStorage.setItem('id_token', authResult.idToken);
  localStorage.setItem('expires_at', expiresAt);

  // schedule a token renewal

  // navigate to the home route

To schedule renewing the tokens when the page is refreshed, in the constructor of the Auth service, add a call to the scheduleRenewal method.

// src/Auth/Auth.js

// ...
constructor() {
  // ...

Since client-side sessions should not be renewed after the user logs out, call clearTimeout in the logout method to cancel the renewal.

// src/Auth/Auth.js

logout() {
  // ...


If you're having problems with token renewal (for example, you get the login_required error), make sure you're not using Auth0 dev keys for social login. You must use your own social authentication keys.

Previous Tutorial
4. Authorization
Was this article helpful?
Use Auth0 for FREECreate free Account