import { HTTP_INTERCEPTORS , HttpEvent , HttpHandler , HttpInterceptor , HttpRequest , } from '@angular/common/http' ; import { Injectable , Provider } from '@angular/core' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { Observable } from 'rxjs' ; import { switchMap , take } from 'rxjs/operators' ; @ Injectable ( ) export class AuthInterceptor implements HttpInterceptor { static provider : Provider = { provide : HTTP_INTERCEPTORS , useClass : AuthInterceptor , multi : true , } ; constructor ( private afa ) { } intercept ( req , next ) { return this . afa . idToken . pipe ( take ( 1 ) , switchMap ( jwt => { const authHeader = `Bearer ${ jwt }` ; const authReq = req . clone ( { setHeaders : { Authorization : authHeader } } ) ; return next . handle ( authReq ) ; } ) ) ; } }