title: Angular - JWT Refresh Token
body: <p>After a long time I didn't find an approach about refresing tokens
  TTL: 30 minutes
  Refresh TTL: 2 Weeks</p>
  
  <p>If I refresh the page after 45 minutes innactive then I make a getAccessToken() function to send the expired token and then send me back a refreshed token. The big problem is that if my page makes more than 1 ajax request then if the first request invalidate my token the second request force my to relogin becauce it send empty token</p>
  
  <pre><code>@NgModule({
  providers: [
      {
          provide: AuthHttp,
          useFactory: authHttpServiceFactory,
          deps: [Http, RequestOptions, Router]
      }
  ]
  })
  
  export function authHttpServiceFactory(http: Http, options: RequestOptions, router: Router) {
  return new AuthHttp(new AuthConfig({
      tokenName: 'token',
      tokenGetter: (() =&gt; getAccessToken(http,router)),
      //tokenGetter: (() =&gt; localStorage.getItem('JWToken')),
      globalHeaders: [{'Content-Type': 'application/json'}],
      noJwtError: true,
  }), http, options);
  }
  
  
  function getAccessToken(http: Http, router:Router): Promise&lt;string&gt; {
  
  let jwtHelper: JwtHelper = new JwtHelper();
  
  let accessToken = localStorage.getItem('JWToken');
  
  if( accessToken == '' || !accessToken || accessToken == undefined || accessToken == null){
      router.navigate(['./admin/login']);
      return;
  }
  
  
  if (jwtHelper.isTokenExpired(accessToken)) {
      return new Promise((resolve, reject) =&gt; {
          let refreshTokenService: RefreshTokenService = new RefreshTokenService(http);
          refreshTokenService.refreshToken(accessToken).subscribe((res: any) =&gt; {
              res = res.json();
  
              if(res.token) {
                  localStorage.setItem('JWToken', res.token);
                  resolve(res.token);
  
              }else{
                  localStorage.removeItem('JWToken');
  
                  router.navigate(['./admin/login']);
              }
          });
      });
  } else {
      return Promise.resolve(accessToken);
  }
  }
  </code></pre>
  
  <p>I want the requests wait for the response of the first request</p>