axios.interceptors.response.use(
response => {
return response
},
function (error) {
const originalRequest = error.config
if (
error.response.status === 401 &&
originalRequest.url === 'http://127.0.0.1:3000/v1/auth/token'
) {
router.push('/login')
return Promise.reject(error)
}
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true
const refreshToken = localStorageService.getRefreshToken()
return axios
.post('/auth/token', {
refresh_token: refreshToken
})
.then(res => {
if (res.status === 201) {
localStorageService.setToken(res.data)
axios.defaults.headers.common['Authorization'] =
'Bearer ' + localStorageService.getAccessToken()
return axios(originalRequest)
}
})
}
return Promise.reject(error)
}
)
以上代码片段的目的是为了在access_token过期之后请求api获取新的access_token,这里拦截器有一个return axios().then 这个语法,这里是返回了一个axios实例吧,这个函数会执行吗?完全看不懂这句话的意思,返回了这个实例会被request 拦截器执行?还是被redux action creator 执行??