28 lines
826 B
TypeScript
28 lines
826 B
TypeScript
import {Injectable} from '@angular/core';
|
|
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
|
|
import {Observable} from 'rxjs';
|
|
|
|
@Injectable()
|
|
export class AuthInterceptor implements HttpInterceptor {
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
// Retrieve the token from localStorage
|
|
const token = localStorage.getItem('token');
|
|
|
|
if (token) {
|
|
// Clone the request and add the Authorization header
|
|
const clonedRequest = req.clone({
|
|
setHeaders: {
|
|
Authorization: `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
// Pass the cloned request to the next handler
|
|
return next.handle(clonedRequest);
|
|
}
|
|
|
|
// If no token, continue without adding the Authorization header
|
|
return next.handle(req);
|
|
}
|
|
}
|