How do $http interceptors work, and how can they be used to handle API authentication?

$http interceptors intercept requests/responses, useful for adding auth tokens or handling errors globally. For API authentication, an interceptor can attach JWT tokens to headers, enhancing maintainability by centralizing auth handling.

app.factory('authInterceptor', function() {
  return {
    request: function(config) {
      config.headers.Authorization = 'Bearer token';
      return config;
    }
  };
});
$httpProvider.interceptors.push('authInterceptor');