Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
Axios response interceptor for refreshing token keeps firing

The logic: On every request there's a JWT Authorization header that authenticates the user. If that expires, there's a cookie endpoint in place ready to refresh the JWT.

I am using axios and interceptor response to check if the client gets a 401 to try and refresh the JWT. The cookie may be valid or not.

The problem is that the interceptor to refresh the JWT never stops firing, and I think I have something wrong with the synchronization of the requests. Below is my code:

function refreshToken(dispatch) {
  return new Promise((resolve, reject) => {
    instance.put('/auth').then((response) => {
      dispatch({ type: "UPDATE_AUTH", payload: response.data });
      resolve(response);
    })
    .catch((error) => {
      reject(error);
    });
  });
}

instance.interceptors.response.use(
    response => {
      return response;
    },
    err => {
      const error = err.response;
      
      if (error.status === 401 && error.config && !error.config._retry) {
        error.config._retry = true;

        return refreshToken(dispatch).then((resp) => {
          return instance(error.config);
        })
        .catch((e) => {
          return Promise.reject(e);
        });
      }
      return Promise.reject(error);
    }
  );
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you have more then one parallel requests, refresh the JWT will be equal to the number of requests. Try to send /auth only first time and prevent next requests. You can use localStorage for this task.

let requests = [];

instance.interceptors.response.use(
  response => {
    return response;
  },
  err => {
    const error = err.response;
    
    if (error.status === 401 && error.config && !error.config._retry) {
      requests.push(error.config);
      if (!localStorage.getItem('refresh')) {
        localStorage.setItem('refresh', 'done');
        error.config._retry = true;

        return refreshToken(dispatch).then((resp) => {
          localStorage.removeItem('refresh');
          
           const token = getAccessToken();
           requests.map(req => {
              req.headers.Authorization = `Bearer ${token}`;
              return instance(req)
           })
        })
        .catch((e) => {
          localStorage.removeItem('refresh');
          return Promise.reject(e);
        });
      }
    } else {
      requests = [];
    }
    return Promise.reject(error);
  }
);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda