仅发送一个刷新JWT请求

问题描述 投票:0回答:1

我想在发送正确的请求之前刷新令牌,但是我的问题是我的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;
    }
  });
javascript reactjs jwt token refresh
1个回答
0
投票

您需要使用锁定。对于浏览器来说,这很棘手,因为您还需要注意跨选项卡锁定。查看我为锁定浏览器而编写的该库:https://www.npmjs.com/package/browser-tabs-lock

[如果您想知道它的用法,您可以看到名为SuperTokens的库如何同步刷新API的调用:https://github.com/supertokens/supertokens-website/blob/master/handleSessionExp.ts(第18-55行在锁内)。

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.