From 1dbb056a58442ee1931dd2840590dbbb82b3514c Mon Sep 17 00:00:00 2001 From: sandre Date: Fri, 16 Jan 2026 15:25:14 +0100 Subject: [PATCH] Fix les interceptors --- Front/skydivelogs-app/src/app/app.config.ts | 5 +- .../src/interceptor/error.interceptor.ts | 74 +++++++++++++------ .../src/interceptor/jwt-auth.interceptor.ts | 36 ++++----- 3 files changed, 69 insertions(+), 46 deletions(-) diff --git a/Front/skydivelogs-app/src/app/app.config.ts b/Front/skydivelogs-app/src/app/app.config.ts index 9cbde66..46428cd 100644 --- a/Front/skydivelogs-app/src/app/app.config.ts +++ b/Front/skydivelogs-app/src/app/app.config.ts @@ -2,7 +2,7 @@ import { inject, provideAppInitializer } from "@angular/core"; import { ApplicationConfig, provideZoneChangeDetection } from "@angular/core"; import { provideRouter } from "@angular/router"; import { DatePipe } from "@angular/common"; -import { HTTP_INTERCEPTORS, provideHttpClient } from "@angular/common/http"; +import { provideHttpClient, withInterceptors } from "@angular/common/http"; import { DateService } from "../services/date.service"; import { AircraftService } from "../services/aircraft.service"; @@ -56,8 +56,7 @@ export const appConfig: ApplicationConfig = { const initializerFn = initConfig(inject(ConfigurationHelper)); return initializerFn(); }), - { provide: HTTP_INTERCEPTORS, useClass: JwtAuthInterceptor, multi: true }, - { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }, + provideHttpClient(withInterceptors([JwtAuthInterceptor, ErrorInterceptor])), provideCharts(withDefaultRegisterables()), provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), diff --git a/Front/skydivelogs-app/src/interceptor/error.interceptor.ts b/Front/skydivelogs-app/src/interceptor/error.interceptor.ts index 8380160..85b566d 100644 --- a/Front/skydivelogs-app/src/interceptor/error.interceptor.ts +++ b/Front/skydivelogs-app/src/interceptor/error.interceptor.ts @@ -1,29 +1,59 @@ -import { Injectable } from '@angular/core'; -import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; -import { Observable, throwError } from 'rxjs'; -import { catchError } from 'rxjs/operators'; +// import { Injectable } from "@angular/core"; +// import { +// HttpRequest, +// HttpHandler, +// HttpEvent, +// HttpInterceptor, +// } from "@angular/common/http"; +// import { Observable, throwError } from "rxjs"; +// import { catchError } from "rxjs/operators"; -import { AuthenticationService } from '../services/authentication.service'; +// import { AuthenticationService } from "../services/authentication.service"; +// @Injectable() +// export class ErrorInterceptor implements HttpInterceptor { +// constructor(private authenticationService: AuthenticationService) {} -@Injectable() -export class ErrorInterceptor implements HttpInterceptor { +// intercept( +// request: HttpRequest, +// next: HttpHandler +// ): Observable> { +// return next.handle(request).pipe( +// catchError((err) => { +// if (err.status === 401) { +// // auto logout if 401 response returned from api +// this.authenticationService.logout(); +// location.reload(); +// } - constructor(private authenticationService: AuthenticationService) { } +// const error = err.error.message || err.statusText; +// return throwError(() => error); +// }) +// ); +// } +// } - intercept(request: HttpRequest, next: HttpHandler): Observable> { - return next.handle(request) - .pipe( - catchError(err => { - if (err.status === 401) { - // auto logout if 401 response returned from api - this.authenticationService.logout(); - location.reload(); - } +import { HttpEvent, HttpHandlerFn, HttpRequest } from "@angular/common/http"; +import { inject } from "@angular/core"; +import { catchError, Observable, throwError } from "rxjs"; +import { AuthenticationService } from "../services/authentication.service"; - const error = err.error.message || err.statusText; - return throwError(error); - }) - ); - } +export function ErrorInterceptor( + request: HttpRequest, + next: HttpHandlerFn +): Observable> { + const authenticationService = inject(AuthenticationService); + + return next(request).pipe( + catchError((err) => { + if (err.status === 401) { + // auto logout if 401 response returned from api + authenticationService.logout(); + location.reload(); + } + + const error = err.error.message || err.statusText; + return throwError(() => error); + }) + ); } diff --git a/Front/skydivelogs-app/src/interceptor/jwt-auth.interceptor.ts b/Front/skydivelogs-app/src/interceptor/jwt-auth.interceptor.ts index 250f257..7aa11d3 100644 --- a/Front/skydivelogs-app/src/interceptor/jwt-auth.interceptor.ts +++ b/Front/skydivelogs-app/src/interceptor/jwt-auth.interceptor.ts @@ -1,27 +1,21 @@ -import { Injectable } from "@angular/core"; -import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from "@angular/common/http"; +import { HttpEvent, HttpHandlerFn, HttpRequest } from "@angular/common/http"; +import { inject } from "@angular/core"; import { Observable } from "rxjs"; - import { AuthenticationService } from "../services/authentication.service"; +export function JwtAuthInterceptor( + request: HttpRequest, + next: HttpHandlerFn +): Observable> { + const currentUser = inject(AuthenticationService).currentUserValue; -@Injectable() -export class JwtAuthInterceptor implements HttpInterceptor { - - constructor(private authenticationService: AuthenticationService) {} - - intercept(request: HttpRequest, next: HttpHandler): Observable> { - // add authorization header with basic auth credentials if available - const currentUser = this.authenticationService.currentUserValue; - - if (currentUser && currentUser.token) { - request = request.clone({ - setHeaders: { - Authorization: `Bearer ${currentUser.token}` - } - }); - } - - return next.handle(request); + if (currentUser && currentUser.token) { + request = request.clone({ + setHeaders: { + Authorization: `Bearer ${currentUser.token}`, + }, + }); } + + return next(request); }