Spaces:
Building
Building
Update flare-ui/src/app/interceptors/auth.interceptor.ts
Browse files
flare-ui/src/app/interceptors/auth.interceptor.ts
CHANGED
@@ -1,35 +1,38 @@
|
|
1 |
-
import { HttpInterceptorFn, HttpErrorResponse } from '@angular/common/http';
|
2 |
-
import { inject } from '@angular/core';
|
3 |
-
import { Router } from '@angular/router';
|
4 |
-
import { catchError, throwError } from 'rxjs';
|
5 |
-
import { AuthService } from '../services/auth.service';
|
6 |
-
|
7 |
-
export const authInterceptor: HttpInterceptorFn = (req, next) => {
|
8 |
-
const authService = inject(AuthService);
|
9 |
-
const router = inject(Router);
|
10 |
-
|
11 |
-
// Skip auth for login endpoint
|
12 |
-
if (req.url.includes('/api/login')) {
|
13 |
-
return next(req);
|
14 |
-
}
|
15 |
-
|
16 |
-
// Add auth token to requests
|
17 |
-
const token = authService.getToken();
|
18 |
-
if (token) {
|
19 |
-
req = req.clone({
|
20 |
-
setHeaders: {
|
21 |
-
Authorization: `Bearer ${token}`
|
22 |
-
}
|
23 |
-
});
|
24 |
-
}
|
25 |
-
|
26 |
-
return next(req).pipe(
|
27 |
-
catchError((error: HttpErrorResponse) => {
|
28 |
-
if (error.status === 401) {
|
29 |
-
authService.logout();
|
30 |
-
router.navigate(['/login']);
|
31 |
-
}
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
};
|
|
|
1 |
+
import { HttpInterceptorFn, HttpErrorResponse } from '@angular/common/http';
|
2 |
+
import { inject } from '@angular/core';
|
3 |
+
import { Router } from '@angular/router';
|
4 |
+
import { catchError, throwError } from 'rxjs';
|
5 |
+
import { AuthService } from '../services/auth.service';
|
6 |
+
|
7 |
+
export const authInterceptor: HttpInterceptorFn = (req, next) => {
|
8 |
+
const authService = inject(AuthService);
|
9 |
+
const router = inject(Router);
|
10 |
+
|
11 |
+
// Skip auth for login endpoint
|
12 |
+
if (req.url.includes('/api/login')) {
|
13 |
+
return next(req);
|
14 |
+
}
|
15 |
+
|
16 |
+
// Add auth token to requests
|
17 |
+
const token = authService.getToken();
|
18 |
+
if (token) {
|
19 |
+
req = req.clone({
|
20 |
+
setHeaders: {
|
21 |
+
Authorization: `Bearer ${token}`
|
22 |
+
}
|
23 |
+
});
|
24 |
+
}
|
25 |
+
|
26 |
+
return next(req).pipe(
|
27 |
+
catchError((error: HttpErrorResponse) => {
|
28 |
+
if (error.status === 401) {
|
29 |
+
authService.logout();
|
30 |
+
router.navigate(['/login']);
|
31 |
+
} else if (error.status === 409) {
|
32 |
+
// Race condition - let components handle this
|
33 |
+
console.warn('Race condition detected:', error.error?.detail);
|
34 |
+
}
|
35 |
+
return throwError(() => error);
|
36 |
+
})
|
37 |
+
);
|
38 |
};
|