Aurelia refresh access token and resend failed request

Viewed 180

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?

0 Answers
Related