added refresh

This commit is contained in:
2025-12-08 12:47:30 +01:00
parent 3bc368ecb3
commit f2a9f24bd5
2 changed files with 37 additions and 3 deletions

View File

@@ -1,9 +1,15 @@
import { HttpInterceptorFn } from '@angular/common/http'; import {
HttpInterceptorFn,
HttpErrorResponse
} from '@angular/common/http';
import { inject } from '@angular/core'; import { inject } from '@angular/core';
import { AuthService } from '../services/auth.service'; import { AuthService } from '../services/auth.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { catchError, throwError } from 'rxjs';
export const authInterceptor: HttpInterceptorFn = (req, next) => { export const authInterceptor: HttpInterceptorFn = (req, next) => {
const authService = inject(AuthService); const authService = inject(AuthService);
const notificationService = inject(NzNotificationService);
const token = authService.getToken(); const token = authService.getToken();
if (token) { if (token) {
@@ -12,5 +18,24 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => {
}); });
} }
return next(req); return next(req).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
notificationService.error(
'Session expirée',
'Veuillez vous reconnecter.'
);
authService.logout?.();
}
if (error.status === 403) {
notificationService.error(
'Accès refusé',
'Vous navez pas les droits pour cette action.'
);
}
return throwError(() => error);
})
);
}; };

View File

@@ -26,4 +26,13 @@ export class AuthService {
getToken(): string | null { getToken(): string | null {
return localStorage.getItem('jwt'); return localStorage.getItem('jwt');
} }
setToken(token: string) {
localStorage.setItem('jwt', token);
}
logout() {
localStorage.removeItem('jwt');
}
} }