import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/internal/Observable' ; import { HttpEvent , HttpHandler , HttpInterceptor , HttpRequest } from '@angular/common/http' ; @ Injectable ( ) export class AuthInterceptor implements HttpInterceptor { getAuthorizationHeader = ( ) => { const token = localStorage . getItem ( 'token' ) ; return token ; } ; intercept ( req , next ) { let authHeader = req . headers . get ( 'Authorization' ) ; let authReq = req ; if ( ! authHeader ) { authHeader = this . getAuthorizationHeader ( ) ; if ( authHeader ) { authReq = req . clone ( { headers : req . headers . set ( 'Authorization' , 'Bearer ' + authHeader ) } ) ; } } return next . handle ( authReq ) ; } }