Aurelia刷新访问令牌并重新发送失败的请求

时间:2020-02-24 13:48:35

标签: jwt aurelia refresh-token

我正在使用具有JWT身份验证的Aurelia,并希望在过期时刷新访问令牌。访问令牌刷新后,失败的请求应重新执行。如果刷新令牌也已过期,则应将用户导航到登录页面。

我目前在main.ts中将其作为拦截器

.withInterceptor({
        request(request) {
          console.log(`Requesting ${request.method} ${request.url}`);
          return request;
        },
        response(response): Response {
          if(response.status == 401 && !localStorage.getItem('isRefreshingToken')){

            if(getRefreshToken()){
              localStorage.setItem('isRefreshingToken', "true");
              let authService = new AuthService(http);

              authService.refreshToken().then(success => {
                localStorage.removeItem('isRefreshingToken');
                console.log("refresh", success);

                //re-execute failed request

              }).catch(failed => {
                localStorage.removeItem('isRefreshingToken');

                //refresh_token is also expired so go to login page

              });
            } else {
              //refresh_token is also expired so go to login page
            }
          }
          console.log(`Received ${response.status} ${response.url}`);
          return response;
        }
      });

令人耳目一新的部分有效,但我不知道如何在拦截器中进行重定向(这是否有可能?/是否应该这样做?)。还有如何重新执行失败的请求?

我尝试了Google和Aurelia文档,但没有得到我要找的答案。有人可以指出我正确的方向吗?

0 个答案:

没有答案