30 lines
883 B
TypeScript
30 lines
883 B
TypeScript
import {HttpInterceptorFn} from '@angular/common/http';
|
|
import {inject} from '@angular/core';
|
|
import {Router} from '@angular/router';
|
|
import {HttpErrorResponse} from '@angular/common/http';
|
|
import {catchError} from 'rxjs/operators';
|
|
import {throwError} from 'rxjs';
|
|
import {AuthService} from './core/service/auth.service';
|
|
|
|
export const AuthInterceptorService: HttpInterceptorFn = (req, next) => {
|
|
const authService = inject(AuthService);
|
|
const router = inject(Router);
|
|
const token = localStorage.getItem('token');
|
|
|
|
if (token) {
|
|
req = req.clone({
|
|
setHeaders: {Authorization: `Bearer ${token}`}
|
|
});
|
|
}
|
|
|
|
return next(req).pipe(
|
|
catchError((error: HttpErrorResponse) => {
|
|
if (error.status === 403 || error.status === 401) {
|
|
authService.logout();
|
|
router.navigate(['/login']);
|
|
}
|
|
return throwError(() => error);
|
|
})
|
|
);
|
|
};
|