1 | import { Inject, Injectable, Injector } from '@angular/core';
|
2 | import { switchMap } from 'rxjs/operators';
|
3 | import { NbAuthService } from '../auth.service';
|
4 | import { NB_AUTH_INTERCEPTOR_HEADER } from '../../auth.options';
|
5 | export 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 | }
|
27 | NbAuthSimpleInterceptor.decorators = [
|
28 | { type: Injectable }
|
29 | ];
|
30 | NbAuthSimpleInterceptor.ctorParameters = () => [
|
31 | { type: Injector },
|
32 | { type: String, decorators: [{ type: Inject, args: [NB_AUTH_INTERCEPTOR_HEADER,] }] }
|
33 | ];
|
34 |
|
\ | No newline at end of file |