How to handle by using axios with token based authentication 401 unauthenticated request? And here, I used axios interceptors request & response. But is the interceptors response I don’t know what can I do?
// import
import axios from "axios";
axios interceptors request
axios.interceptors.request.use(
config => {
const accessToken = localStorage.getItem("ACCESS-TOKEN");
if (accessToken) {
config.headers['Authorization'] = 'Bearer ' + accessToken;
}
config.headers['Content-Type'] = 'application/json';
return config;
},
error => {
return Promise.reject(error);
}
);
axios interceptors response
axios.interceptors.response.use(
response => {
return response.data;
},
error => {
const originalRequest = error.config;
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
const refreshToken = localStorage.getItem("REFRESH-TOKEN");
localStorage.removeItem("ACCESS-TOKEN");
// ?????
// how to handle
// ?????
}
return Promise.reject(error);
}
);