diff --git a/frontend/src/app/core/interceptors/auth.interceptor.spec.ts b/frontend/src/app/core/interceptors/auth.interceptor.spec.ts index 7b5ef4d..fbb8254 100644 --- a/frontend/src/app/core/interceptors/auth.interceptor.spec.ts +++ b/frontend/src/app/core/interceptors/auth.interceptor.spec.ts @@ -68,6 +68,18 @@ describe('authInterceptor', () => { req.flush([]); }); + it('ajoute le Bearer même quand l URL de l API est absolue (apiUrl relatif /api résolu sur l origine)', () => { + // En dev, apiUrl = '/api' : au moment de l intercepteur, l URL est déjà résolue + // en absolu par rapport à l origine (ex. http://localhost:4200/api/...). Le Bearer + // doit tout de même être ajouté (régression corrigée via le test includes('/api/')). + const url = 'http://localhost:4200/api/trajets'; + http.get(url).subscribe(); + + const req = httpMock.expectOne(url); + expect(req.request.headers.get('Authorization')).toBe('Bearer jwt-abc'); + req.flush([]); + }); + it('n ajoute pas le Bearer sur un endpoint public d auth (/auth/login)', () => { const url = `${environment.apiUrl}/auth/login`; http.post(url, { email: 'a@b.fr', motDePasse: 'x' }).subscribe(); diff --git a/frontend/src/app/core/interceptors/auth.interceptor.ts b/frontend/src/app/core/interceptors/auth.interceptor.ts index ef6c87b..03cc300 100644 --- a/frontend/src/app/core/interceptors/auth.interceptor.ts +++ b/frontend/src/app/core/interceptors/auth.interceptor.ts @@ -35,7 +35,11 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => { const authService = inject(AuthService); const router = inject(Router); - const cibleApi = req.url.startsWith(environment.apiUrl); + // Détection de l'API robuste au cas où `apiUrl` est relatif (`/api`) : à ce stade + // Angular a déjà résolu `req.url` en URL absolue (ex. http://localhost:4200/api/...), + // donc un simple `startsWith('/api')` serait faux. On teste aussi la présence du + // segment `/api/` dans l'URL, comme le fait le timeoutInterceptor. + const cibleApi = req.url.startsWith(environment.apiUrl) || req.url.includes('/api/'); const estEndpointPublic = ENDPOINTS_AUTH_PUBLICS.some((chemin) => req.url.includes(chemin)); const token = authService.getToken();