import { Injectable } from '@angular/core' ; import { HttpRequest , HttpHandler , HttpEvent , HttpInterceptor } from '@angular/common/http' ; import { Observable , throwError } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class TokenInterceptor implements HttpInterceptor { constructor ( ) { } intercept ( request , next ) { const storage = localStorage . getItem ( 'authToken' ) ; if ( ! storage ) { return next . handle ( request ) . pipe ( catchError ( this . handleError ) ) ; } request = request . clone ( { setHeaders : { Authorization : `${ localStorage . getItem ( 'authToken' ) }` } } ) ; return next . handle ( request ) . pipe ( catchError ( this . handleError ) ) ; } public handleError = ( error ) => { return throwError ( error ) ; } ; }