Fix les interceptors
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user