diff options
author | Chocobozzz <me@florianbigard.com> | 2020-06-23 14:10:17 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-06-23 16:00:49 +0200 |
commit | 67ed6552b831df66713bac9e672738796128d33f (patch) | |
tree | 59c97d41e0b49d75a90aa3de987968ab9b1ff447 /client/src/app/shared/auth/auth-interceptor.service.ts | |
parent | 0c4bacbff53bc732f5a2677d62a6ead7752e2405 (diff) | |
download | PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.gz PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.zst PeerTube-67ed6552b831df66713bac9e672738796128d33f.zip |
Reorganize client shared modules
Diffstat (limited to 'client/src/app/shared/auth/auth-interceptor.service.ts')
-rw-r--r-- | client/src/app/shared/auth/auth-interceptor.service.ts | 60 |
1 files changed, 0 insertions, 60 deletions
diff --git a/client/src/app/shared/auth/auth-interceptor.service.ts b/client/src/app/shared/auth/auth-interceptor.service.ts deleted file mode 100644 index bb236bf8c..000000000 --- a/client/src/app/shared/auth/auth-interceptor.service.ts +++ /dev/null | |||
@@ -1,60 +0,0 @@ | |||
1 | import { Observable, throwError as observableThrowError } from 'rxjs' | ||
2 | import { catchError, switchMap } from 'rxjs/operators' | ||
3 | import { Injectable, Injector } from '@angular/core' | ||
4 | import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' | ||
5 | import { AuthService } from '../../core' | ||
6 | |||
7 | @Injectable() | ||
8 | export class AuthInterceptor implements HttpInterceptor { | ||
9 | private authService: AuthService | ||
10 | |||
11 | // https://github.com/angular/angular/issues/18224#issuecomment-316957213 | ||
12 | constructor (private injector: Injector) {} | ||
13 | |||
14 | intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
15 | if (this.authService === undefined) { | ||
16 | this.authService = this.injector.get(AuthService) | ||
17 | } | ||
18 | |||
19 | const authReq = this.cloneRequestWithAuth(req) | ||
20 | |||
21 | // Pass on the cloned request instead of the original request | ||
22 | // Catch 401 errors (refresh token expired) | ||
23 | return next.handle(authReq) | ||
24 | .pipe( | ||
25 | catchError(err => { | ||
26 | if (err.status === 401 && err.error && err.error.code === 'invalid_token') { | ||
27 | return this.handleTokenExpired(req, next) | ||
28 | } | ||
29 | |||
30 | return observableThrowError(err) | ||
31 | }) | ||
32 | ) | ||
33 | } | ||
34 | |||
35 | private handleTokenExpired (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
36 | return this.authService.refreshAccessToken() | ||
37 | .pipe( | ||
38 | switchMap(() => { | ||
39 | const authReq = this.cloneRequestWithAuth(req) | ||
40 | |||
41 | return next.handle(authReq) | ||
42 | }) | ||
43 | ) | ||
44 | } | ||
45 | |||
46 | private cloneRequestWithAuth (req: HttpRequest<any>) { | ||
47 | const authHeaderValue = this.authService.getRequestHeaderValue() | ||
48 | |||
49 | if (authHeaderValue === null) return req | ||
50 | |||
51 | // Clone the request to add the new header | ||
52 | return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) }) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | export const AUTH_INTERCEPTOR_PROVIDER = { | ||
57 | provide: HTTP_INTERCEPTORS, | ||
58 | useClass: AuthInterceptor, | ||
59 | multi: true | ||
60 | } | ||