1 | import { Inject, Injectable, Injector } from '@angular/core';
|
2 | import { switchMap } from 'rxjs/operators';
|
3 | import { NbAuthService } from '../auth.service';
|
4 | import { NB_AUTH_TOKEN_INTERCEPTOR_FILTER } from '../../auth.options';
|
5 | export class NbAuthJWTInterceptor {
|
6 | constructor(injector, filter) {
|
7 | this.injector = injector;
|
8 | this.filter = filter;
|
9 | }
|
10 | intercept(req, next) {
|
11 |
|
12 | if (!this.filter(req)) {
|
13 | return this.authService.isAuthenticatedOrRefresh()
|
14 | .pipe(switchMap(authenticated => {
|
15 | if (authenticated) {
|
16 | return this.authService.getToken().pipe(switchMap((token) => {
|
17 | const JWT = `Bearer ${token.getValue()}`;
|
18 | req = req.clone({
|
19 | setHeaders: {
|
20 | Authorization: JWT,
|
21 | },
|
22 | });
|
23 | return next.handle(req);
|
24 | }));
|
25 | }
|
26 | else {
|
27 |
|
28 |
|
29 | return next.handle(req);
|
30 | }
|
31 | }));
|
32 | }
|
33 | else {
|
34 | return next.handle(req);
|
35 | }
|
36 | }
|
37 | get authService() {
|
38 | return this.injector.get(NbAuthService);
|
39 | }
|
40 | }
|
41 | NbAuthJWTInterceptor.decorators = [
|
42 | { type: Injectable }
|
43 | ];
|
44 | NbAuthJWTInterceptor.ctorParameters = () => [
|
45 | { type: Injector },
|
46 | { type: undefined, decorators: [{ type: Inject, args: [NB_AUTH_TOKEN_INTERCEPTOR_FILTER,] }] }
|
47 | ];
|
48 |
|
\ | No newline at end of file |