import { Injectable } from '@angular/core' ; import { HttpInterceptor , HttpRequest , HttpHandler , HttpSentEvent , HttpHeaderResponse , HttpProgressEvent , HttpResponse , HttpUserEvent , HttpErrorResponse } from '@angular/common/http' ; import { Observable } from 'rxjs/Observable' ; import { Router } from '@angular/router' ; import { TokenStorage } from './token.storage' ; import 'rxjs/add/operator/do' ; const TOKEN_HEADER_KEY = 'Authorization' ; @ Injectable ( ) export class Interceptor implements HttpInterceptor { constructor ( private token , private router ) { } intercept ( req , next ) { let authReq = req ; if ( this . token . getToken ( ) != null ) { authReq = req . clone ( { headers : req . headers . set ( TOKEN_HEADER_KEY , 'Bearer ' + this . token . getToken ( ) ) } ) ; } return next . handle ( authReq ) . do ( ( err ) => { if ( err instanceof HttpErrorResponse ) { console . log ( err ) ; console . log ( 'req url :: ' + req . url ) ; if ( err . status === 401 ) { this . router . navigate ( [ 'user' ] ) ; } } } ) ; } }