diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-01-24 03:02:04 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-01-25 15:37:26 +0100 |
commit | ab398a05e9ffaacb8fc713bb2ba9717ac463b34c (patch) | |
tree | d73c6debfcc14c0dc05c681bb442e6f6fb6ce3a7 /client/src/app/shared/shared-main/auth | |
parent | 6939cbac48e0a9823b34577836480ae3c28186be (diff) | |
download | PeerTube-ab398a05e9ffaacb8fc713bb2ba9717ac463b34c.tar.gz PeerTube-ab398a05e9ffaacb8fc713bb2ba9717ac463b34c.tar.zst PeerTube-ab398a05e9ffaacb8fc713bb2ba9717ac463b34c.zip |
redirect to login on 401, display error variants in 404 component
Diffstat (limited to 'client/src/app/shared/shared-main/auth')
-rw-r--r-- | client/src/app/shared/shared-main/auth/auth-interceptor.service.ts | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts b/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts index 68a4acdb5..3ddaffbdf 100644 --- a/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts +++ b/client/src/app/shared/shared-main/auth/auth-interceptor.service.ts | |||
@@ -1,15 +1,17 @@ | |||
1 | import { Observable, throwError as observableThrowError } from 'rxjs' | 1 | import { Observable, of, throwError as observableThrowError } from 'rxjs' |
2 | import { catchError, switchMap } from 'rxjs/operators' | 2 | import { catchError, switchMap } from 'rxjs/operators' |
3 | import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' | 3 | import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpErrorResponse } from '@angular/common/http' |
4 | import { Injectable, Injector } from '@angular/core' | 4 | import { Injectable, Injector } from '@angular/core' |
5 | import { AuthService } from '@app/core/auth/auth.service' | 5 | import { AuthService } from '@app/core/auth/auth.service' |
6 | import { Router } from '@angular/router' | ||
7 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
6 | 8 | ||
7 | @Injectable() | 9 | @Injectable() |
8 | export class AuthInterceptor implements HttpInterceptor { | 10 | export class AuthInterceptor implements HttpInterceptor { |
9 | private authService: AuthService | 11 | private authService: AuthService |
10 | 12 | ||
11 | // https://github.com/angular/angular/issues/18224#issuecomment-316957213 | 13 | // https://github.com/angular/angular/issues/18224#issuecomment-316957213 |
12 | constructor (private injector: Injector) {} | 14 | constructor (private injector: Injector, private router: Router) {} |
13 | 15 | ||
14 | intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | 16 | intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
15 | if (this.authService === undefined) { | 17 | if (this.authService === undefined) { |
@@ -22,9 +24,11 @@ export class AuthInterceptor implements HttpInterceptor { | |||
22 | // Catch 401 errors (refresh token expired) | 24 | // Catch 401 errors (refresh token expired) |
23 | return next.handle(authReq) | 25 | return next.handle(authReq) |
24 | .pipe( | 26 | .pipe( |
25 | catchError(err => { | 27 | catchError((err: HttpErrorResponse) => { |
26 | if (err.status === 401 && err.error && err.error.code === 'invalid_token') { | 28 | if (err.status === HttpStatusCode.UNAUTHORIZED_401 && err.error && err.error.code === 'invalid_token') { |
27 | return this.handleTokenExpired(req, next) | 29 | return this.handleTokenExpired(req, next) |
30 | } else if (err.status === HttpStatusCode.UNAUTHORIZED_401) { | ||
31 | return this.handleNotAuthenticated(err) | ||
28 | } | 32 | } |
29 | 33 | ||
30 | return observableThrowError(err) | 34 | return observableThrowError(err) |
@@ -51,6 +55,11 @@ export class AuthInterceptor implements HttpInterceptor { | |||
51 | // Clone the request to add the new header | 55 | // Clone the request to add the new header |
52 | return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) }) | 56 | return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) }) |
53 | } | 57 | } |
58 | |||
59 | private handleNotAuthenticated (err: HttpErrorResponse, path = '/login'): Observable<any> { | ||
60 | this.router.navigateByUrl(path) | ||
61 | return of(err.message) | ||
62 | } | ||
54 | } | 63 | } |
55 | 64 | ||
56 | export const AUTH_INTERCEPTOR_PROVIDER = { | 65 | export const AUTH_INTERCEPTOR_PROVIDER = { |