title: Angular 5 Http Interceptor refreshing JWT token
body: <p>I already have implemented logic of token saving, retrieving and I have refreshing call also. The problem is that when I intercept 403 in my HttpInterceptor, other calls that are made at the same time, also refresh the token. I would love to hold those calls until my token is refreshed. To create what I would call a 'semaphore' of requests.</p>
  
  <pre><code>@Injectable()
  export class TokenInterceptor implements HttpInterceptor {
  
  private auth: AuthService;
  
  constructor(private injector: Injector) {
  }
  
  intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
      this.auth = this.injector.get(AuthService);
  
      if(this.auth.isAuthenticated()){
          request = request.clone({
              setHeaders: {
                  Accept: 'application/json',
                  Authorization: `Bearer ${localStorage.getItem('access_token')}`
              }
          });
      } else {
          request = request.clone({
              setHeaders: {
                  Accept: 'application/json'
              }
          });
      }
  
      return next.handle(request).catch(error =&gt; {
          if (error.status === 401) {
              console.log('refreshing token');
  
              // TODO: return Refresh Token here and hold other calls
          }
  
          return Observable.throw(error);
      });
  }
  </code></pre>