4

I'm using Aurelia with JWT authentication and want to refesh the access token when it is expired. When the access token has been refreshed the failed request should be re-executed. If the refresh token is also expired the user should be navigated to the login page.

I currently have this as the interceptor in 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;
        }
      });

The refreshing part works but i don't know how to redirect in the interceptor (is this even possibl? / should this even be done?). Also how i can re-execute the failed request?

I tried Google and the Aurelia documentation, but didnt get the answers i'm looking for. Can someone point me in the right direction?

RJP
  • 41
  • 2
  • 2
    There's been this PR merged https://github.com/aurelia/fetch-client/pull/104. You shjould be able to use retry functionality with fetch client, could you give it a try? – bigopon Feb 25 '20 at 12:33
  • @bigopon this looks promising, thanks. I will soon report if it actualy works for me. – RJP Feb 28 '20 at 12:18

0 Answers0