import { Injectable , Injector } from '@angular/core' ; import { HttpRequest , HttpHandler , HttpEvent , HttpInterceptor } from '@angular/common/http' ; import { AuthService } from './auth.service' ; import { Observable } from 'rxjs/Observable' ; import { switchMap } from 'rxjs/operators' ; import * as firebase from 'firebase/app' ; @ Injectable ( ) export class TokenInterceptor implements HttpInterceptor { auth : AuthService ; constructor ( private inj ) { } intercept ( request , next ) { if ( request . url . indexOf ( 'oauthCallback' ) > - 1 ) { return next . handle ( request ) ; } this . auth = this . inj . get ( AuthService ) ; return this . auth . getUserIdToken ( ) . pipe ( switchMap ( token => { request = request . clone ( { setHeaders : { Authorization : `Bearer ${ token }` } } ) ; return next . handle ( request ) ; } ) ) ; } }