title: Angular 6 HTTP Interceptor Refresh token 401 handling
body: <p>I have built my AuthInterceptor which on a 401 error sends a request to get a new token.</p>
  
  <p>The handle401Error method is called when I encounter a 401 error, But I don't seem to be getting any response in my this.authService.refreshToken().pipe()</p>
  
  <p><strong>AuthInterceptor</strong></p>
  
  <pre><code>import { Injectable, Injector } from '@angular/core';
  import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
  import { Observable, throwError } from 'rxjs';
  import { catchError, tap, switchMap } from 'rxjs/operators';
  import { AuthService } from '../services/auth.service';
  
  @Injectable()
  export class AuthInterceptor implements HttpInterceptor {
    isRefreshingToken:boolean = false;
    constructor(public authService: AuthService, private injector: Injector) {}
  
      addToken(req: HttpRequest&lt;any&gt;, token: string): HttpRequest&lt;any&gt; {
          return req.clone({ setHeaders: {
            Authorization: 'Bearer ' + token,
            'Content-Type': 'application/json'
         }})
      }
  
    intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
  
      request = this.addToken(request, this.authService.getToken())
      return next.handle(request).pipe (
        catchError((error: Error) =&gt; {
          if(error instanceof HttpErrorResponse){
            console.log('err', error);
            switch(error.status) {
              case 401:
              this.handle401Error(request, next)
              default:
              return throwError(error);
            }
  
          } else {
            return throwError(error);
          }
        })
      );
    }
  
    handle401Error(request: HttpRequest&lt;any&gt;, next: HttpHandler){
      console.log('handle 401');
      if(!this.isRefreshingToken) { //do not send another refresh token request if already in progress
        this.isRefreshingToken = true;
  
        return this.authService.refreshToken().pipe(
          switchMap((newToken: string) =&gt; {
            console.log('newToken', newToken) // I DO NOT GET A VALUE HERE
              if (newToken) { 
                return next.handle(this.addToken(request, newToken));
              }
  
          }),
          catchError((error) =&gt; {
            console.log('error', error) //NOR DO I GET AN ERROR
            return throwError(error)
          }),
          tap(data =&gt; console.log(data))
        )
      }
    }
  }
  </code></pre>
  
  <p><strong>AuthService Refresh Token</strong></p>
  
  <pre><code>refreshToken(): Observable&lt;string&gt; {
      console.log('refreshToken');
      let token = 'xxx-xxx-xx';
  
      return of(token).pipe(delay(1000));
  }
  </code></pre>