import { Injectable } from '@angular/core' ; import { HttpRequest , HttpHandler , HttpEvent , HttpInterceptor } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { switchMap } from 'rxjs/operators' ; import { OidcFacade } from 'ng-oidc-client' ; @ Injectable ( { providedIn : 'root' } ) export class OidcInterceptorService implements HttpInterceptor { static OidcInterceptorService : any ; constructor ( private oidcFacade ) { } intercept ( req , next ) { return this . oidcFacade . identity$ . pipe ( switchMap ( user => { if ( user && ! user . expired && user . access_token ) { req = req . clone ( { setHeaders : { Authorization : `Bearer ${ user . access_token }` } } ) ; } return next . handle ( req ) ; } ) ) ; } }