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

100
Views
How to excute http request synchronously inside axios request interceptor

I'm trying to integrate a refresh token request within an axios interceptor whenever the jwt is expired, but it looks like the http request is only executed after the interceptor.

The fact that the request to refresh the jwt is not executed synchronously, it breaks my code and I have like an infinite loop because I never get a new access token.

What can I implement to easily implement a refresh token part into my interceptor ?

Here's my code :

axios.interceptors.request.use(request => {
            /** Token informations **/
            const token = localStorage.getItem('token');
            const token_refresh = localStorage.getItem('token_refresh');
            const tokenIsExpired = isExpired(token)
            /** Token validity checks **/
            if (tokenIsExpired || token === null) {
                if (token_refresh !== null) {
                    const unInterceptedAxiosInstance = axios.create();
                    unInterceptedAxiosInstance.post('/token_refresh', {token_refresh})
                        .then(({data: {token, token_refresh, user}}) => {
                            console.log('successful refresh');
                            setupAuthEnvironment(token, token_refresh, user)
                            token = token;
                        }).catch(() => {
                        console.log('expired refresh..')
                        // if token_refresh is expired in database
                        logout();
                    })
                } else {
                    logout();
                }
            }
            // Set header with token
            axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
            console.log(request);
            return request;
        }
about 4 years ago · Juan Pablo Isaza
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!