diff options
Diffstat (limited to 'client/src/app/shared/auth')
-rw-r--r-- | client/src/app/shared/auth/auth-interceptor.service.ts | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/client/src/app/shared/auth/auth-interceptor.service.ts b/client/src/app/shared/auth/auth-interceptor.service.ts index efcfc452b..bb236bf8c 100644 --- a/client/src/app/shared/auth/auth-interceptor.service.ts +++ b/client/src/app/shared/auth/auth-interceptor.service.ts | |||
@@ -1,14 +1,8 @@ | |||
1 | import { Observable, throwError as observableThrowError } from 'rxjs' | ||
2 | import { catchError, switchMap } from 'rxjs/operators' | ||
1 | import { Injectable, Injector } from '@angular/core' | 3 | import { Injectable, Injector } from '@angular/core' |
2 | import { | 4 | import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' |
3 | HttpInterceptor, | ||
4 | HttpRequest, | ||
5 | HttpEvent, | ||
6 | HttpHandler, HTTP_INTERCEPTORS | ||
7 | } from '@angular/common/http' | ||
8 | import { Observable } from 'rxjs/Observable' | ||
9 | |||
10 | import { AuthService } from '../../core' | 5 | import { AuthService } from '../../core' |
11 | import 'rxjs/add/operator/switchMap' | ||
12 | 6 | ||
13 | @Injectable() | 7 | @Injectable() |
14 | export class AuthInterceptor implements HttpInterceptor { | 8 | export class AuthInterceptor implements HttpInterceptor { |
@@ -27,22 +21,26 @@ export class AuthInterceptor implements HttpInterceptor { | |||
27 | // Pass on the cloned request instead of the original request | 21 | // Pass on the cloned request instead of the original request |
28 | // Catch 401 errors (refresh token expired) | 22 | // Catch 401 errors (refresh token expired) |
29 | return next.handle(authReq) | 23 | return next.handle(authReq) |
30 | .catch(err => { | 24 | .pipe( |
31 | if (err.status === 401 && err.error && err.error.code === 'invalid_token') { | 25 | catchError(err => { |
32 | return this.handleTokenExpired(req, next) | 26 | if (err.status === 401 && err.error && err.error.code === 'invalid_token') { |
33 | } | 27 | return this.handleTokenExpired(req, next) |
34 | 28 | } | |
35 | return Observable.throw(err) | 29 | |
36 | }) | 30 | return observableThrowError(err) |
31 | }) | ||
32 | ) | ||
37 | } | 33 | } |
38 | 34 | ||
39 | private handleTokenExpired (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | 35 | private handleTokenExpired (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
40 | return this.authService.refreshAccessToken() | 36 | return this.authService.refreshAccessToken() |
41 | .switchMap(() => { | 37 | .pipe( |
42 | const authReq = this.cloneRequestWithAuth(req) | 38 | switchMap(() => { |
39 | const authReq = this.cloneRequestWithAuth(req) | ||
43 | 40 | ||
44 | return next.handle(authReq) | 41 | return next.handle(authReq) |
45 | }) | 42 | }) |
43 | ) | ||
46 | } | 44 | } |
47 | 45 | ||
48 | private cloneRequestWithAuth (req: HttpRequest<any>) { | 46 | private cloneRequestWithAuth (req: HttpRequest<any>) { |