Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!