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;
}