【问题标题】:Send only one refresh JWT request只发送一个刷新 JWT 请求
【发布时间】:2020-05-25 14:08:24
【问题描述】:

我想在发送正确请求之前刷新令牌,但我的反应应用程序在 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】:

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

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 1970-01-01
      • 2018-02-09
      • 2017-10-19
      • 2016-11-06
      • 1970-01-01
      • 2015-06-22
      • 2022-08-09
      • 1970-01-01
      相关资源
      最近更新 更多