From c361143ba29823a96b9a92c824d10126ed86e8de Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Tue, 20 Apr 2021 21:54:01 +0200 Subject: [PATCH] Fix interceptor for unauthorized http response (#27) --- CHANGELOG.md | 1 + apps/client/src/app/core/auth.interceptor.ts | 16 +--------------- .../src/app/core/http-response.interceptor.ts | 11 ++++++++++- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0797d765..de28bc585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed the frozen screen if the token has expired - Fixed some issues in the generic scraper ## 0.87.0 - 19.04.2021 diff --git a/apps/client/src/app/core/auth.interceptor.ts b/apps/client/src/app/core/auth.interceptor.ts index 2f61a7ae6..f6e586f68 100644 --- a/apps/client/src/app/core/auth.interceptor.ts +++ b/apps/client/src/app/core/auth.interceptor.ts @@ -45,21 +45,7 @@ export class AuthInterceptor implements HttpInterceptor { authReq = req.clone({ headers }); } - return next.handle(authReq).pipe( - tap( - () => {}, - (err: any) => { - if (err instanceof HttpErrorResponse) { - if (err.status !== 401) { - return; - } - - this.tokenStorageService.signOut(); - this.router.navigate(['start']); - } - } - ) - ); + return next.handle(authReq); } } diff --git a/apps/client/src/app/core/http-response.interceptor.ts b/apps/client/src/app/core/http-response.interceptor.ts index 770b0710f..a44db3320 100644 --- a/apps/client/src/app/core/http-response.interceptor.ts +++ b/apps/client/src/app/core/http-response.interceptor.ts @@ -15,15 +15,21 @@ import { MatSnackBarRef, TextOnlySnackBar } from '@angular/material/snack-bar'; +import { Router } from '@angular/router'; import { StatusCodes } from 'http-status-codes'; import { Observable, throwError } from 'rxjs'; import { catchError, tap } from 'rxjs/operators'; +import { TokenStorageService } from '../services/token-storage.service'; @Injectable() export class HttpResponseInterceptor implements HttpInterceptor { public snackBarRef: MatSnackBarRef; - public constructor(private snackBar: MatSnackBar) {} + public constructor( + private router: Router, + private tokenStorageService: TokenStorageService, + private snackBar: MatSnackBar + ) {} public intercept( request: HttpRequest, @@ -70,6 +76,9 @@ export class HttpResponseInterceptor implements HttpInterceptor { window.location.reload(); }); } + } else if (error.status === StatusCodes.UNAUTHORIZED) { + this.tokenStorageService.signOut(); + this.router.navigate(['start']); } return throwError('');