import { Injectable } from "@angular/core"; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, } from "@angular/common/http"; import { Observable } from "rxjs/Rx"; import "rxjs/add/observable/throw"; import "rxjs/add/operator/catch"; import { Defaults } from "./default.constants"; import { ImpersonateHelper } from "./impersonate.helper"; @Injectable() export class TokenHttpInterceptor implements HttpInterceptor { constructor() {} intercept( req: HttpRequest, next: HttpHandler ): Observable> { let headers = {}; // Handle Impersonate const impersonateUser = ImpersonateHelper.getImpersonateUserId(); if (impersonateUser) headers[Defaults.IMPERSONATE_ID_HEADER] = impersonateUser; // Set Request Headers req = req.clone({ setHeaders: headers }); if (window.navigator.userAgent.search(/MSIE|Trident/i) != -1) { req.headers.set("Access-Control-Allow-Headers", "*"); req.headers.set("Cache-Control", "no-store"); req.headers.set("Cache-Control", "no-cache"); req.headers.set("Pragma", "no-cache"); } return next.handle(req).catch((error, caught) => { return Observable.throw(error); }) as any; } }