make interceptor redirect to login page and after login redirect back to what you get 401
This commit is contained in:
parent
ad32fe38ea
commit
ea506b8128
1 changed files with 5 additions and 2 deletions
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue