diff --git a/app.config.ts b/app.config.ts index 665969215ae063def606833bc5bfb37f9110112f..dc39b0fc17bc3c32d8997ea8a6820efb85e0fd8d 100644 --- a/app.config.ts +++ b/app.config.ts @@ -9,21 +9,16 @@ import { ApplicationConfig } from '@angular/core'; import { provideProtractorTestingSupport } from '@angular/platform-browser'; import { provideRouter } from '@angular/router'; -import { - provideHttpClient, - withInterceptorsFromDi, -} from '@angular/common/http'; -import { HTTP_INTERCEPTORS } from '@angular/common/http'; +import { provideHttpClient, withInterceptors } from '@angular/common/http'; import { provideAnimations } from '@angular/platform-browser/animations'; import routeConfig from 'src/app/routes'; -import { DevAuthInterceptor } from 'src/app/core/interceptors/dev-auth-interceptor.service'; +import { devAuthInterceptor } from 'src/app/core/interceptors/dev-auth-interceptor.service'; export const appConfig: ApplicationConfig = { providers: [ provideProtractorTestingSupport(), provideRouter(routeConfig), provideAnimations(), - { provide: HTTP_INTERCEPTORS, useClass: DevAuthInterceptor, multi: true }, - provideHttpClient(withInterceptorsFromDi()), + provideHttpClient(withInterceptors([devAuthInterceptor])), ], }; diff --git a/src/app/core/interceptors/dev-auth-interceptor.service.ts b/src/app/core/interceptors/dev-auth-interceptor.service.ts index f249311a68494227bdac5fd4873c4405aa1b5c42..4af97ec9a21c02abf020053fcf6852b07d3579e8 100644 --- a/src/app/core/interceptors/dev-auth-interceptor.service.ts +++ b/src/app/core/interceptors/dev-auth-interceptor.service.ts @@ -1,33 +1,34 @@ -import { Injectable } from '@angular/core'; +import { inject } from '@angular/core'; import { - HttpEvent, - HttpHandler, - HttpInterceptor, + HttpHandlerFn, + HttpInterceptorFn, HttpRequest, } from '@angular/common/http'; -import { Observable } from 'rxjs'; +import { switchMap } from 'rxjs/operators'; import { environment } from 'src/environments/environment'; import { JwtTokenService } from '../services/auth/jwt-token.service'; -@Injectable() -export class DevAuthInterceptor implements HttpInterceptor { - constructor(protected jwtTokenService: JwtTokenService) {} - intercept( - request: HttpRequest<unknown>, - next: HttpHandler, - ): Observable<HttpEvent<unknown>> { - // if the system is not in production, change the bearer for each http request - if (!environment.production) { - const modifiedReq = request.clone({ - headers: request.headers.set( - 'Authorization', - `Bearer ${this.jwtTokenService.getToken()}`, - ), - }); - return next.handle(modifiedReq); - } +export const devAuthInterceptor: HttpInterceptorFn = ( + request: HttpRequest<unknown>, + next: HttpHandlerFn, +) => { + const jwtTokenService = inject(JwtTokenService); - // if there is nothing to intercept, pass the request. - return next.handle(request); + if (environment.production) { + return next(request); } -} + + return jwtTokenService.getToken().pipe( + switchMap((token) => { + if (token) { + // Ensure token is a non-null string + const authReq = request.clone({ + headers: request.headers.set('Authorization', `Bearer ${token}`), + }); + return next(authReq); + } + // If no token, forward the original request + return next(request.clone()); + }), + ); +};