【发布时间】:2021-10-24 03:50:31
【问题描述】:
我正在开发一个更大的网络应用程序,它会从不同的地方多次调用服务器。我们正在使用一个生命周期为 15 分钟的令牌。这意味着它使用 refreshtoken 来生成一个新的令牌,并在它过期时生成一个新的 refreshtoken。我们正在使用 axios 的拦截器来查看令牌是否已过期。基于此,我们正在执行调用以生成新的调用。
现在的问题是,在我们请求获取新令牌期间进行的所有 API 调用都将失败!
所以我现在的问题是是否可以在特定调用完全执行之前阻止发送请求(使用 axios)?
export const runRefreshToken = (errorCallback: () => void): void => {
Axios.interceptors.response.use(
(response: AxiosResponse<any>): AxiosResponse<any> => {
return response;
}, (error: any): any => {
const original = error.config;
const loginVerification = !(original.url as string).startsWith("api/login");
if (error.response && error.response.status === 401 && error.config && loginVerification && !error.config.__retry) {
return new Promise((resolve): void => {
original.__retry = true;
const response = fetch("api/login/refresh-token", { method: 'POST' })
.then((): AxiosPromise<any> => {
return Axios(original);
})
.catch((): void => errorCallback())
resolve(response);
});
} else if (error.config.__retry && original.url !== "api/login/verify-authentication") {
history.push("/login");
}
return Promise.reject(error);
}
);
}
提前致谢
【问题讨论】:
标签: reactjs typescript axios