我如何使用axios为多个请求刷新身份验证令牌日志?

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

我试图这样做,但是对于三个请求,它发送了三个刷新请求:

  • 1、2、3失败并显示401
  • 刷新成功,1次成功,2次,3次失败
  • 刷新成功,2次成功,3次失败
  • 刷新成功3成功

我无法在移动设备上施加那么多的负载(即使只有3次刷新而没有“ refail”)

这是我的代码:

function requestRefreshToken(refreshToken, accessToken) {
  return axios
    .create({
      baseURL: apiUrl + Endpoints.AUTH.REFRESH,
      skipAuthRefresh: true,
      headers: {
        'Accept-Language': 'ru',
        'User-Agent': `${Platform.OS} ${packageJson.version}`,
        Authorization: accessToken,
      },
    })
    .post(
      '',
      {
        grant_type: GrantTypes.REFRESH_TOKEN,
        refresh_token: refreshToken,
        client_id: Client.id,
        client_secret: Client.secret,
      },
      { validateStatus },
    );
}

const refreshAuthLogic = async failedRequest =>
  Keychain.getCredentials()
    .then(old => requestRefreshToken(old.refreshToken, old.accessToken))
    .then(({ data: credentials }) => {

      failedRequest.config.headers.Authorization = `${credentials.token_type} ${
        credentials.access_token
      }`;

      return Keychain.setCredentials(credentials);
    });

createAuthRefreshInterceptor(axios, refreshAuthLogic, {
  retryInstance: axios,
  skipWhileRefreshing: true,
  onRetry: function(config) {
    return Keychain.getCredentials().then(({ accessToken }) =>
      axios({
        ...config,
        header: { ...config.headers, Authorization: accessToken },
      }),
    );
  },
});

axios.interceptors.response.use(
  r => r,
  request => {
    if (request.response.status === 401) {
      return Keychain.getCredentials().then(({ accessToken }) =>
        axios({
          ...request.config,
          header: { ...request.config.headers, Authorization: accessToken },
        }),
      );
    }
  },
);
axios refresh-token
1个回答
0
投票

已解决!似乎没有失败的请求队列就无法解决它(https://gist.github.com/mkjiau/650013a99c341c9f23ca00ccb213db1c

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