git init v2
This commit is contained in:
25
src/app/interceptors/auth-interceptor.ts
Normal file
25
src/app/interceptors/auth-interceptor.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { HttpInterceptorFn } from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import {AuthService} from '../services/auth.service';
|
||||
|
||||
const excludedUrls = ['/auth/login'];
|
||||
|
||||
export const AuthInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
const authService = inject(AuthService);
|
||||
const token = authService.getToken();
|
||||
|
||||
if (excludedUrls.some(url => req.url.includes(url))) {
|
||||
return next(req);
|
||||
}
|
||||
|
||||
if (token) {
|
||||
const cloned = req.clone({
|
||||
setHeaders: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
return next(cloned);
|
||||
}
|
||||
|
||||
return next(req);
|
||||
};
|
||||
Reference in New Issue
Block a user