【发布时间】:2019-11-25 00:31:57
【问题描述】:
我在更新令牌以重新执行原始请求后遇到问题。 基本上,在获得并正确设置新令牌后,当我尝试执行原始请求时,我不断收到 Request failed with status code 401。
我确定我搞砸了承诺,但我不知道问题出在哪里
// axiosWrapper.js
import axios from 'axios';
import authenticationApi from './authentication';
const baseURL = 'http://apiurl/';
const axiosWrapper = axios.create({
baseURL,
});
axiosWrapper.interceptors.response.use(
response => response,
error => {
if (error.response.status === 401) {
return authenticationApi.auth()
.then(res => {
if (res.status === 200) {
setToken(res.data.token);
return axios.request(error.config);
}
})
.catch(err => {
console.log(err); // Request failed with status code 401
});
}
return Promise.reject(error);
},
);
axiosWrapper.interceptors.request.use(async config => {
if (config.url === '/auth') {
return config;
}
var token = await getToken();
config.headers.Authorization = token ? 'Bearer ${token}' : '';
return config;
});
export default axiosWrapper;
// authentication.js
import axiosWrapper from './axiosWrapper';
export default {
auth: () => axiosWrapper.post('auth', {apiKey: 'apiKey'}),
};
【问题讨论】:
标签: javascript react-native axios