From 691ffbabfd071085103f6ebce0d43fa95874cd28 Mon Sep 17 00:00:00 2001
From: kaw67872 <kawtar.laariche@iais.fraunhofer.de>
Date: Thu, 18 Apr 2024 12:29:40 +0200
Subject: [PATCH] #17:  update devAuthInterceptor(use function instead of
 class)

---
 app.config.ts                                 | 11 ++--
 .../dev-auth-interceptor.service.ts           | 51 ++++++++++---------
 2 files changed, 29 insertions(+), 33 deletions(-)

diff --git a/app.config.ts b/app.config.ts
index 6659692..dc39b0f 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 f249311..4af97ec 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());
+    }),
+  );
+};
-- 
GitLab