Skip to content

Instantly share code, notes, and snippets.

@smorstabilini
Created April 16, 2020 13:05
Show Gist options
  • Save smorstabilini/7ea9596633e594c2aeb19cec5c4caf83 to your computer and use it in GitHub Desktop.
Save smorstabilini/7ea9596633e594c2aeb19cec5c4caf83 to your computer and use it in GitHub Desktop.
axiosInstance.interceptors.response.use(
response => response,
error => {
const originalRequest = error.config;
if (error.response.status === 401 && error.response.statusText === "Unauthorized"
&& error.response.data.code === "token_not_valid") {
const refreshToken = localStorage.getItem('refresh_token');
if (refreshToken) {
const tokenParts = JSON.parse(atob(refreshToken.split('.')[1]));
// exp date in token is expressed in seconds, while now() returns milliseconds:
const now = Math.ceil(Date.now() / 1000);
if (tokenParts.exp > now) {
return axiosInstance
.post('/token/refresh/', {refresh: refreshToken})
.then((response) => {
localStorage.setItem('access_token', response.data.access);
localStorage.setItem('refresh_token', response.data.refresh);
axiosInstance.defaults.headers['Authorization'] = "JWT " + response.data.access;
originalRequest.headers['Authorization'] = "JWT " + response.data.access;
return axiosInstance(originalRequest);
})
.catch(err => {
console.log(err)
});
} else {
console.log("Refresh token is expired", tokenParts.exp, now);
}
} else {
console.log("no refresh token");
}
} // else error is different from 401
return Promise.reject(error);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment