I want to implement JWT authentication in a react app using Axios.
There are many solutions using axios.interceptors that fetch the token again if the request failed due to the authentication error.
Example:
axios.interceptors.response.use(function(response) {
return response;
},function(error) {
const originalReq = error.config;
if ( error.response.status == 401 &&
!originalReq._retry &&
error.response.config.url != URL_USER_AUTHENTICATE ) {
originalReq._retry = true;
return axios.post(BASE_URL+URL_REFRESH_TOKEN,{})
.then((res) =>{
if ( res.data.status == "success") {
return axios(originalReq);
}
}).catch((error) => {window.location.href="/logout/nosession"});
}
return Promise.reject(error);
});
However, it doesn't work if we have parallel requests. Imagine that we have two parallel requests, so both will request the new authentication token.
Does anybody know any fix?