X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-main%2Fauth%2Fauth-interceptor.service.ts;h=93b3a93d6d4255def5a42c0f606353c05fb98f96;hb=d0fbc9fd0a29c37f3ff9b99030351e90b276fe7d;hp=3ddaffbdf35f2cc8b51e70fa532309326332b71d;hpb=ab398a05e9ffaacb8fc713bb2ba9717ac463b34c;p=github%2FChocobozzz%2FPeerTube.git 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 3ddaffbdf..93b3a93d6 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,10 +1,11 @@ import { Observable, of, throwError as observableThrowError } from 'rxjs' import { catchError, switchMap } from 'rxjs/operators' -import { HTTP_INTERCEPTORS, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpErrorResponse } from '@angular/common/http' +import { HTTP_INTERCEPTORS, HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' import { Injectable, Injector } from '@angular/core' -import { AuthService } from '@app/core/auth/auth.service' import { Router } from '@angular/router' -import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' +import { AuthService } from '@app/core/auth/auth.service' +import { HttpStatusCode } from '@shared/models' +import { OAuth2ErrorCode, PeerTubeProblemDocument } from '@shared/models/server' @Injectable() export class AuthInterceptor implements HttpInterceptor { @@ -25,13 +26,20 @@ export class AuthInterceptor implements HttpInterceptor { return next.handle(authReq) .pipe( catchError((err: HttpErrorResponse) => { - if (err.status === HttpStatusCode.UNAUTHORIZED_401 && err.error && err.error.code === 'invalid_token') { - return this.handleTokenExpired(req, next) - } else if (err.status === HttpStatusCode.UNAUTHORIZED_401) { - return this.handleNotAuthenticated(err) + const error = err.error as PeerTubeProblemDocument + const isOTPMissingError = this.authService.isOTPMissingError(err) + + if (!isOTPMissingError) { + if (err.status === HttpStatusCode.UNAUTHORIZED_401 && error && error.code === OAuth2ErrorCode.INVALID_TOKEN) { + return this.handleTokenExpired(req, next) + } + + if (err.status === HttpStatusCode.UNAUTHORIZED_401) { + return this.handleNotAuthenticated(err) + } } - return observableThrowError(err) + return observableThrowError(() => err) }) ) } @@ -56,8 +64,8 @@ export class AuthInterceptor implements HttpInterceptor { return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) }) } - private handleNotAuthenticated (err: HttpErrorResponse, path = '/login'): Observable { - this.router.navigateByUrl(path) + private handleNotAuthenticated (err: HttpErrorResponse): Observable { + this.router.navigate([ '/401' ], { state: { obj: err }, skipLocationChange: true }) return of(err.message) } }