01Cross-cutting concerns belong in one place

Authentication, error reporting, retries and global loading indicators apply to almost every request. When they live in feature services, they drift: one screen shows a friendly error, another shows nothing, and a third logs the user out on any 401.

Angular's functional interceptors give these concerns one home. Feature services describe what to fetch; the interceptor chain decides how every request is authenticated, retried and reported.

02What the chain handles

  1. 01

    Auth header on every API call

    One interceptor adds the token for your API's origin only — never for third-party URLs, which would leak it.

  2. 02

    401 means re-authenticate, once

    Refresh or redirect to login in one place. Feature code never checks status codes for auth.

  3. 03

    Consistent, human error messages

    Map status codes to messages once and show them through the app's notification service, with the same tone everywhere.

  4. 04

    Opt-outs through HttpContext

    Some calls handle their own errors, like an inline "username taken" check. An HttpContextToken lets that request skip the global toast.

  5. 05

    Order is explicit

    withInterceptors([auth, errors, loading]) runs in array order. Put auth first so retries and error handling see the real request.

  6. 06

    Services stay thin

    A service method becomes one line: the URL, the params and the response type. That's what makes them easy to test and review.

03Why functional interceptors

Class-based interceptors still work, but functional interceptors are simpler: a plain function using inject() for its dependencies, registered in app.config.ts next to everything else. They're easy to unit test with TestBed.runInInjectionContext.

On an accounting SaaS platform, the authentication interceptor pipeline and role-based route guards were part of the same effort: access rules live in configuration and interceptors, not re-implemented in each component.

04Auth and errors, with an opt-out

The auth interceptor only touches requests to our own API. The error interceptor shows one consistent message, unless the request opted out through HttpContext.

core/http/interceptors.ts
export const SKIP_ERROR_TOAST = new HttpContextToken<boolean>(() => false);

export const authInterceptor: HttpInterceptorFn = (req, next) => {
  if (!req.url.startsWith(environment.apiUrl)) return next(req);
  const token = inject(AuthService).token();
  return next(token ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req);
};

export const errorInterceptor: HttpInterceptorFn = (req, next) => {
  const notify = inject(NotifyService);
  const auth = inject(AuthService);
  return next(req).pipe(
    catchError((err: HttpErrorResponse) => {
      if (err.status === 401) auth.redirectToLogin();
      else if (!req.context.get(SKIP_ERROR_TOAST)) notify.error(messageFor(err));
      return throwError(() => err);
    }),
  );
};

// app.config.ts
provideHttpClient(withInterceptors([authInterceptor, errorInterceptor]))

// a call that handles its own error inline
this.http.get(url, { context: new HttpContext().set(SKIP_ERROR_TOAST, true) });

The takeaway

Put auth, error handling and loading state in a short chain of functional interceptors, use HttpContext for the few exceptions, and keep feature services to a line or two. Consistency stops depending on every developer remembering every rule.

Written by Jaymin MahetaSenior UI/UX Developer turning Figma designs into fast, accessible enterprise apps in Angular and React.
About me →