make interceptor redirect to login page and after login redirect back to what you get 401

This commit is contained in:
Kenta420 2023-10-23 16:40:40 +07:00
parent ad32fe38ea
commit ea506b8128

View file

@ -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<unknown>,
@ -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,
},
});
}