Centralize HTTP auth headers with AngularJS $http interceptors
Register one AngularJS $http interceptor to attach bearer tokens globally instead of repeating headers in every call.
24 Apr 2026, 12:27 UTC

The useful answer
If every $http call in your AngularJS application manually adds an Authorization header, you are repeating logic that belongs in one place. Register an $http interceptor in a config block so the bearer token is attached automatically to outgoing requests and handled centrally when a response fails.
Takeaway: use $httpProvider.interceptors.push(...) with a factory that returns request and responseError hooks. Always return the config object or a rejected promise; returning undefined breaks the request chain.
How the mechanism works
An interceptor is a factory registered with $httpProvider.interceptors. AngularJS calls its request hook before each $http request leaves the browser and its response or responseError hook after a response arrives. Request hooks run in registration order; response hooks run in reverse registration order. This ordering matters when multiple interceptors touch headers or handle errors.
The hooks receive a config object (request) or a response/rejection object (response). A request hook may return the config, a modified config, or a promise. A response hook may return the response or a rejection through $q.reject().
Worked configuration: attach a bearer token
Place this in your module's config block, before the application bootstraps. No special server permissions are required; edit the client source file and redeploy.
angular.module('myApp').config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(['$q', function($q) {
return {
request: function(config) {
var token = sessionStorage.getItem('authToken');
if (token) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + token;
}
return config;
},
responseError: function(rejection) {
if (rejection && rejection.status === 401) {
sessionStorage.removeItem('authToken');
}
return $q.reject(rejection);
}
};
}]);
}]);
Replace 'myApp' with your module name and 'authToken' with the session-storage key your login flow uses. The interceptor reads the token on every $http.get(), $http.post(), and $resource call because those services build on $http.
Execution order comparison
| Hook type | Execution order | Typical use |
|---|---|---|
request | Registration order | Inject headers, transform payload |
response | Reverse registration order | Normalize successful payloads |
responseError | Reverse registration order | Handle 401, log failures, reject |
If two interceptors set Authorization, the one registered later runs later for requests and can override the earlier value. For responses, the last-registered interceptor sees the error first.
Limits and common mistakes
- Returning undefined: a request hook that returns nothing breaks the promise chain and the request never completes. Always return
configor$q.reject(...). - Mutating shared config: the config object can be reused across requests. Setting headers directly can leak values to unrelated calls. Copy the config when you need to modify it conditionally.
- Global scope: interceptors apply to every
$httpcall. Excluding one endpoint requires a custom config flag, such asconfig.skipAuth === true, checked inside the hook. - Infinite retry loops: retrying a failed request inside
responseErrorwithout clearing the failing condition can loop forever. - End of life: AngularJS is end-of-life and receives no new security patches. Confirm the runtime version with
angular.version.fullbefore relying on this pattern in a security-sensitive application.
Practical verification
- Register a temporary logging interceptor that prints
config.urlinrequestandresponse.statusinresponse. - Load the app and confirm the console output appears for every
$http.get()call. - In a unit test, inject
$httpBackend, flush a request, and assert that theAuthorizationheader was sent. - Check
angular.version.fullin the browser console to record the AngularJS version in use.
This change alters runtime behavior but does not write persistent state. To roll back, remove the $httpProvider.interceptors.push(...) call, rebuild, and redeploy.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.