src/app/auth/secure.interceptor.ts
Methods |
constructor(auth: AuthService)
|
||||||
Defined in src/app/auth/secure.interceptor.ts:8
|
||||||
Parameters :
|
intercept | |||||||||
intercept(req: HttpRequest
|
|||||||||
Defined in src/app/auth/secure.interceptor.ts:12
|
|||||||||
Parameters :
Returns :
Observable<HttpEvent<any>>
|
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs';
import { filter, mergeMap } from 'rxjs/operators';
@Injectable()
export class InterceptorService implements HttpInterceptor {
constructor(private auth: AuthService) { }
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
// @NOTE: If you have some endpoints that are public
// and do not need Authorization header, implement logic
// here to accommodate that and conditionally let public
// requests pass through based on your requirements
return this.auth.token$
.pipe(
filter(token => typeof token === 'string'),
mergeMap(token => {
const tokenReq = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
return next.handle(tokenReq);
})
);
}
}