我想在发送正确的请求之前做刷新令牌,但我的react应用程序在1个视图中发送2-5个请求有问题。通过这种方式,我发送了2-5个刷新令牌的请求。
如何在收到刷新响应之前停止所有请求?
我的代码:
axios.interceptors.request.use(config => {
const decoded = decodeJWT(window.localStorage.getItem("jwt"));
if (decoded && Date.now() >= decoded.exp * 1000) {
return axiosInstance
.post("auth/jwt/refresh/", {
refresh: window.localStorage.getItem("refresh")
})
.then(response => {
window.localStorage.setItem("jwt", response.data.access);
config.headers.Authorization = `JWT ${response.data.access}`;
return config;
})
.catch(err => {
window.localStorage.removeItem("jwt");
return config;
});
} else {
return config;
}
});发布于 2020-02-11 01:28:55
您需要使用锁定。对于浏览器来说,这很棘手,因为您还需要注意跨选项卡锁定。查看我为在浏览器中锁定而编写的这个库:https://www.npmjs.com/package/browser-tabs-lock
如果你想知道它是如何使用的,你可以看看一个名为SuperTokens的库是如何同步调用刷新API的:https://github.com/supertokens/supertokens-website/blob/master/handleSessionExp.ts (第18-55行在一个锁中)。
希望这能有所帮助
https://stackoverflow.com/questions/60147313
复制相似问题