Adding Auth Headers
Attach tokens to outgoing requests.
Why intercept for auth
Most APIs require an Authorization header on every request. Rather than adding it manually to each call, an interceptor injects it automatically for all outgoing requests.
Cloning to add a header
Because requests are immutable, clone with setHeaders to attach the token.
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).getToken();
const authReq = req.clone({
setHeaders: { Authorization: 'Bearer ' + token }
});
return next(authReq);
};All lessons in this course
- Functional Interceptors
- Adding Auth Headers
- Handling Errors Globally
- Retry and Loading Indicators