An HTTP interceptor
Implement an HTTP interceptor to inject the JWT into the header of every request sent to the API, and gracefully handle authentication failures by asking the user to log back in:
- Create an
AuthHttpInterceptor
underauth
:src/app/auth/auth.http.interceptor.ts import { HttpHandlerFn, HttpRequest } from '@angular/common/http' import { inject } from '@angular/core' import { Router } from '@angular/router' import { throwError } from 'rxjs' import { catchError } from 'rxjs/operators' import { environment } from 'src/environments/environment' import { UiService } from '../common/ui.service' import { AuthService } from './auth.service' export function AuthHttpInterceptor( req: HttpRequest<unknown>, next: HttpHandlerFn ) { const authService = inject(AuthService) const router = inject(Router) const uiService = inject(UiService) const jwt = authService.getToken...