UNPKG

1.15 kBJavaScriptView Raw
1import { Inject, Injectable, Injector } from '@angular/core';
2import { switchMap } from 'rxjs/operators';
3import { NbAuthService } from '../auth.service';
4import { NB_AUTH_INTERCEPTOR_HEADER } from '../../auth.options';
5export class NbAuthSimpleInterceptor {
6 constructor(injector, headerName = 'Authorization') {
7 this.injector = injector;
8 this.headerName = headerName;
9 }
10 intercept(req, next) {
11 return this.authService.getToken()
12 .pipe(switchMap((token) => {
13 if (token && token.getValue()) {
14 req = req.clone({
15 setHeaders: {
16 [this.headerName]: token.getValue(),
17 },
18 });
19 }
20 return next.handle(req);
21 }));
22 }
23 get authService() {
24 return this.injector.get(NbAuthService);
25 }
26}
27NbAuthSimpleInterceptor.decorators = [
28 { type: Injectable }
29];
30NbAuthSimpleInterceptor.ctorParameters = () => [
31 { type: Injector },
32 { type: String, decorators: [{ type: Inject, args: [NB_AUTH_INTERCEPTOR_HEADER,] }] }
33];
34//# sourceMappingURL=simple-interceptor.js.map
\No newline at end of file