import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { LocalStorageService , SessionStorageService } from 'ngx-webstorage' ; import { HttpInterceptor , HttpRequest , HttpHandler , HttpEvent } from '@angular/common/http' ; import { SERVER_API_URL } from 'app/app.constants' ; @ Injectable ( ) export class AuthInterceptor implements HttpInterceptor { constructor ( private localStorage , private sessionStorage ) { } intercept ( request , next ) { if ( ! request || ! request . url || ( /^http/ . test ( request . url ) && ! ( SERVER_API_URL && request . url . startsWith ( SERVER_API_URL ) ) ) ) { return next . handle ( request ) ; } const token = this . localStorage . retrieve ( 'authenticationToken' ) || this . sessionStorage . retrieve ( 'authenticationToken' ) ; if ( ! ! token ) { request = request . clone ( { setHeaders : { Authorization : 'Bearer ' + token } } ) ; } return next . handle ( request ) ; } }