Initial
This commit is contained in:
71
listify-client/src/app/auth/auth.interceptor.ts
Normal file
71
listify-client/src/app/auth/auth.interceptor.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
HttpErrorResponse,
|
||||
HttpEvent,
|
||||
HttpHandlerFn,
|
||||
HttpInterceptorFn,
|
||||
HttpRequest,
|
||||
} from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { Observable, catchError, switchMap, throwError } from 'rxjs';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
export const authInterceptor: HttpInterceptorFn = (
|
||||
request: HttpRequest<unknown>,
|
||||
next: HttpHandlerFn,
|
||||
): Observable<HttpEvent<unknown>> => {
|
||||
const auth = inject(AuthService);
|
||||
const router = inject(Router);
|
||||
const authenticatedRequest = withAccessToken(request, auth.accessToken());
|
||||
|
||||
return next(authenticatedRequest).pipe(
|
||||
catchError((error: unknown) => {
|
||||
if (!shouldRefresh(request, error)) {
|
||||
return throwError(() => error);
|
||||
}
|
||||
|
||||
return auth.refreshSession().pipe(
|
||||
switchMap((response) =>
|
||||
next(withAccessToken(request, response.accessToken)),
|
||||
),
|
||||
catchError((refreshError: unknown) => {
|
||||
auth.logout();
|
||||
void router.navigateByUrl('/login');
|
||||
return throwError(() => refreshError);
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
function withAccessToken(
|
||||
request: HttpRequest<unknown>,
|
||||
accessToken: string | null,
|
||||
): HttpRequest<unknown> {
|
||||
if (!accessToken || !isApiRequest(request) || isAuthRequest(request)) {
|
||||
return request;
|
||||
}
|
||||
|
||||
return request.clone({
|
||||
setHeaders: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function shouldRefresh(request: HttpRequest<unknown>, error: unknown): boolean {
|
||||
return (
|
||||
isApiRequest(request) &&
|
||||
!isAuthRequest(request) &&
|
||||
error instanceof HttpErrorResponse &&
|
||||
error.status === 401
|
||||
);
|
||||
}
|
||||
|
||||
function isApiRequest(request: HttpRequest<unknown>): boolean {
|
||||
return request.url.startsWith('/api/');
|
||||
}
|
||||
|
||||
function isAuthRequest(request: HttpRequest<unknown>): boolean {
|
||||
return request.url.startsWith('/api/auth/');
|
||||
}
|
||||
Reference in New Issue
Block a user