- Create UserCreation service

- Logout and redirect to login page when token expires
This commit is contained in:
aditya.chandel
2025-03-07 21:34:35 -07:00
parent 2f15e5ce09
commit 39616dabd7
6 changed files with 155 additions and 96 deletions
@@ -1,16 +1,29 @@
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) {
const clonedReq = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
req = req.clone({
setHeaders: {Authorization: `Bearer ${token}`}
});
return next(clonedReq);
}
return next(req);
return next(req).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 403 || error.status === 401) {
authService.logout();
router.navigate(['/login']);
}
return throwError(() => error);
})
);
};