From ea506b8128a229a7cd5609de59bd11fd66d65eb5 Mon Sep 17 00:00:00 2001 From: Kenta420 Date: Mon, 23 Oct 2023 16:40:40 +0700 Subject: [PATCH] make interceptor redirect to login page and after login redirect back to what you get 401 --- client/src/app/core/interceptors/error.interceptor.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/src/app/core/interceptors/error.interceptor.ts b/client/src/app/core/interceptors/error.interceptor.ts index a7debf1..6187488 100644 --- a/client/src/app/core/interceptors/error.interceptor.ts +++ b/client/src/app/core/interceptors/error.interceptor.ts @@ -9,10 +9,11 @@ import { } from '@angular/common/http'; import { Observable, catchError, throwError } from 'rxjs'; import { Router } from '@angular/router'; +import { UserService } from '../services/user.service'; @Injectable({ providedIn: 'root' }) export class ErrorInterceptor implements HttpInterceptor { - constructor(private router: Router) {} + constructor(private router: Router, private userService: UserService) {} intercept( request: HttpRequest, @@ -21,10 +22,12 @@ export class ErrorInterceptor implements HttpInterceptor { return next.handle(request).pipe( catchError((error) => { if (error instanceof HttpErrorResponse) { + console.log('HTTP Response Error', error); if (error.status == HttpStatusCode.Unauthorized) { + this.userService.purgeAuth(); this.router.navigate(['/login'], { queryParams: { - redirectUrl: request.url, + redirectUrl: this.router.url, }, }); }