Fix les interceptors

This commit is contained in:
2026-01-16 15:25:14 +01:00
parent 0b7408a230
commit 1dbb056a58
3 changed files with 69 additions and 46 deletions

View File

@@ -2,7 +2,7 @@ import { inject, provideAppInitializer } from "@angular/core";
import { ApplicationConfig, provideZoneChangeDetection } from "@angular/core"; import { ApplicationConfig, provideZoneChangeDetection } from "@angular/core";
import { provideRouter } from "@angular/router"; import { provideRouter } from "@angular/router";
import { DatePipe } from "@angular/common"; 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 { DateService } from "../services/date.service";
import { AircraftService } from "../services/aircraft.service"; import { AircraftService } from "../services/aircraft.service";
@@ -56,8 +56,7 @@ export const appConfig: ApplicationConfig = {
const initializerFn = initConfig(inject(ConfigurationHelper)); const initializerFn = initConfig(inject(ConfigurationHelper));
return initializerFn(); return initializerFn();
}), }),
{ provide: HTTP_INTERCEPTORS, useClass: JwtAuthInterceptor, multi: true }, provideHttpClient(withInterceptors([JwtAuthInterceptor, ErrorInterceptor])),
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
provideCharts(withDefaultRegisterables()), provideCharts(withDefaultRegisterables()),
provideZoneChangeDetection({ eventCoalescing: true }), provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes), provideRouter(routes),

View File

@@ -1,29 +1,59 @@
import { Injectable } from '@angular/core'; // import { Injectable } from "@angular/core";
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; // import {
import { Observable, throwError } from 'rxjs'; // HttpRequest,
import { catchError } from 'rxjs/operators'; // 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() // intercept(
export class ErrorInterceptor implements HttpInterceptor { // request: HttpRequest<any>,
// next: HttpHandler
// ): Observable<HttpEvent<any>> {
// 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<any>, next: HttpHandler): Observable<HttpEvent<any>> { import { HttpEvent, HttpHandlerFn, HttpRequest } from "@angular/common/http";
return next.handle(request) import { inject } from "@angular/core";
.pipe( import { catchError, Observable, throwError } from "rxjs";
catchError(err => { import { AuthenticationService } from "../services/authentication.service";
export function ErrorInterceptor(
request: HttpRequest<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
const authenticationService = inject(AuthenticationService);
return next(request).pipe(
catchError((err) => {
if (err.status === 401) { if (err.status === 401) {
// auto logout if 401 response returned from api // auto logout if 401 response returned from api
this.authenticationService.logout(); authenticationService.logout();
location.reload(); location.reload();
} }
const error = err.error.message || err.statusText; const error = err.error.message || err.statusText;
return throwError(error); return throwError(() => error);
}) })
); );
} }
}

View File

@@ -1,27 +1,21 @@
import { Injectable } from "@angular/core"; import { HttpEvent, HttpHandlerFn, HttpRequest } from "@angular/common/http";
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from "@angular/common/http"; import { inject } from "@angular/core";
import { Observable } from "rxjs"; import { Observable } from "rxjs";
import { AuthenticationService } from "../services/authentication.service"; import { AuthenticationService } from "../services/authentication.service";
export function JwtAuthInterceptor(
@Injectable() request: HttpRequest<unknown>,
export class JwtAuthInterceptor implements HttpInterceptor { next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
constructor(private authenticationService: AuthenticationService) {} const currentUser = inject(AuthenticationService).currentUserValue;
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with basic auth credentials if available
const currentUser = this.authenticationService.currentUserValue;
if (currentUser && currentUser.token) { if (currentUser && currentUser.token) {
request = request.clone({ request = request.clone({
setHeaders: { setHeaders: {
Authorization: `Bearer ${currentUser.token}` Authorization: `Bearer ${currentUser.token}`,
} },
}); });
} }
return next.handle(request); return next(request);
}
} }