import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http'; import { Router } from '@angular/router'; import { catchError, Observable, throwError } from 'rxjs'; import { AuthService } from './auth.service'; @Injectable({ providedIn: 'root' }) export class AuthInterceptor implements HttpInterceptor { constructor( private authService: AuthService, private router: Router) { } intercept(req: HttpRequest, next: HttpHandler): Observable> { // get the auth token var token = this.authService.getToken(); // if the token is present, clone the request // replacing the original headers with the authorization if (token) { req = req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }); } // send the request to the next handler return next.handle(req).pipe( catchError((error) => { // Perform logout on 401 - Unauthorized HTTP response errors if (error instanceof HttpErrorResponse && error.status === 401) { this.authService.logout(); this.router.navigate(['login']); } return throwError(error); }) ); } }