|
|
|
@ -5,12 +5,11 @@ import {
|
|
|
|
|
HttpEvent,
|
|
|
|
|
HttpErrorResponse,
|
|
|
|
|
} from '@angular/common/http';
|
|
|
|
|
import { Observable, from, throwError } from 'rxjs';
|
|
|
|
|
import { Observable, from, of, throwError } from 'rxjs';
|
|
|
|
|
import { catchError, switchMap } from 'rxjs/operators';
|
|
|
|
|
import { inject } from '@angular/core';
|
|
|
|
|
import { CookiesService } from './services/cookies/cookies.service';
|
|
|
|
|
import { ModalService } from './services/modal/modal.service';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { AuthService } from './services/auth/auth.service';
|
|
|
|
|
|
|
|
|
|
export const AuthInterceptor: HttpInterceptorFn = (
|
|
|
|
|
req: HttpRequest<unknown>,
|
|
|
|
@ -22,9 +21,7 @@ export const AuthInterceptor: HttpInterceptorFn = (
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cookiesService = inject(CookiesService);
|
|
|
|
|
const router = inject(Router);
|
|
|
|
|
const modalService = inject(ModalService);
|
|
|
|
|
|
|
|
|
|
const authService = inject(AuthService);
|
|
|
|
|
return from(cookiesService.getValidToken()).pipe(
|
|
|
|
|
switchMap((token) => {
|
|
|
|
|
const authReq = token
|
|
|
|
@ -37,10 +34,7 @@ export const AuthInterceptor: HttpInterceptorFn = (
|
|
|
|
|
}),
|
|
|
|
|
catchError((err: HttpErrorResponse) => {
|
|
|
|
|
if (err.status === 401) {
|
|
|
|
|
cookiesService.clearSession();
|
|
|
|
|
router.navigate(['/']).then(() => {
|
|
|
|
|
modalService.openModal('login-modal');
|
|
|
|
|
});
|
|
|
|
|
authService.logout(false);
|
|
|
|
|
}
|
|
|
|
|
return throwError(() => err);
|
|
|
|
|
})
|
|
|
|
|