import { HttpInterceptorFn } from '@angular/common/http'; import { inject } from '@angular/core'; import { catchError, throwError } from 'rxjs'; import { AuthService } from '../services/auth'; export const authInterceptor: HttpInterceptorFn = (req, next) => { const auth = inject(AuthService); const isInternal = req.url.startsWith('/api'); const token = auth.getToken(); const sessionKey = auth.getSessionKey(); const headers: Record = {}; if (token && isInternal) headers['Authorization'] = `Bearer ${token}`; if (sessionKey && isInternal) headers['X-Session-Key'] = sessionKey; const authReq = Object.keys(headers).length > 0 ? req.clone({ setHeaders: headers }) : req; return next(authReq).pipe( catchError((err) => { if (err.status === 401 && isInternal) { auth.logout(); } return throwError(() => err); }) ); };