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 { 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),

View File

@@ -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<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>> {
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<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
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);
})
);
}

View File

@@ -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<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
const currentUser = inject(AuthenticationService).currentUserValue;
@Injectable()
export class JwtAuthInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}
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) {
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);
}