added interceptor and authService to manage log

This commit is contained in:
2026-03-24 19:08:34 +01:00
parent 491c57b061
commit ef2afb0b58
66 changed files with 1815 additions and 777 deletions
+46
View File
@@ -0,0 +1,46 @@
import {HttpInterceptorFn, HttpErrorResponse, HttpRequest, HttpHandlerFn} from '@angular/common/http';
import {inject} from '@angular/core';
import {catchError, switchMap, throwError} from 'rxjs';
import {AuthManageService} from "../services/auth-manage";
import {AuthService} from "../services/api";
export const authInterceptor: HttpInterceptorFn = (req: HttpRequest<any>, next: HttpHandlerFn) => {
const authManageService = inject(AuthManageService);
const authService = inject(AuthService);
const token = authManageService.getToken();
let authReq = req;
if (token) {
authReq = req.clone({
setHeaders: {Authorization: `Bearer ${token}`}
});
}
return next(authReq).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401 && token) {
return authService.refreshTokenEndpoint({token})
.pipe(
switchMap((res: any) => {
authManageService.setToken(res.token);
const newReq = req.clone({
setHeaders: {Authorization: `Bearer ${res.token}`}
});
return next(newReq);
}),
catchError((refreshErr) => {
authManageService.logout();
console.log('Session expirée', 'Veuillez vous reconnecter.');
return throwError(() => refreshErr);
})
);
}
if (error.status === 403) {
console.log('Accès refusé', 'Vous navez pas les droits pour cette action.');
}
return throwError(() => error);
})
);
};