DEV Community

christian0125
christian0125

Posted on

How can I re-call API in case of axios intercepter error

I am developing react project using JWT authentication and write this code.

const service = axios.create({
  baseURL: API_BASE_URL
  timeout: 60000
});

service.interceptors.response.use( (response) => {
    return response.data
}, async (error) => {

        const originalRequest = error.config;
    
    // Remove token and redirect 
    if (error.response.status === 400 || error.response.status === 403) {

Top comments (1)

Collapse
 
ecyrbe profile image
ecyrbe • Edited

Yes you can. But to evoid Infinite loop, or sending Bad credentials when renewing, do not call your axios instance that you named service (who then will again call your interceptors).

But instead call axios default instance or a fresh one and be sure you did not plug an interceptor to it.

Take a look here from my zodios lib