1 回答
TA贡献1860条经验 获得超8个赞
我最终得到了一个我认为比检查时间戳更可靠的解决方案。谢谢@Bergi,但我担心系统时钟。我使用 axios 拦截器刷新 401 上的令牌
// Request interceptor for API calls
axios.interceptors.request.use(
async config => {
config.headers = {
'Authorization': `Bearer ${localStorage.getItem("accessToken")}`,
'Accept': 'application/json',
}
return config;
},
error => {
Promise.reject(error)
});
// Allow automatic updating of access token
axios.interceptors.response.use(response => response, async (error) => {
const originalRequest = error.config;
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
const res = await axios.post('/users/token', { token: localStorage.getItem('refreshToken') });
setToken(res.data.accessToken);
return axios.request(originalRequest);
}
return Promise.reject(error);
});
改编自https://thedutchlab.com/blog/using-axios-interceptors-for-refreshing-your-api-token
添加回答
举报