我有这个代码将accessToken添加到所有请求:
axios.interceptors.request.use(function (config) {
config.params = config.params || {};
config.params.access_token = localStorage.getItem('access_token');
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
我有拦截器检查所有 401 错误
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (error.response.status === 401) {
if (error.response.data.error === 'invalid_token') {
history.push('/login');
} else if (error.response.data.error === 'token_expired') {
//self.props.dispatch(OauthAction.tokenExpired());
}
}
return Promise.reject(error);
});
当令牌过期时,如何通过refreshToken获取新的访问令牌,并使用新令牌重新发送所有崩溃调度?
例如,当此请求崩溃时,我需要获取新令牌,我的功能必须再次在页面上发送更新数据请求:
export function getNews() {
return (dispatch => {
axios.get(Config.DOMAIN + 'news')
.then(response => {
dispatch(setNews(response.data))
})
})
}