Last active
December 21, 2015 01:29
-
-
Save acroca/6227680 to your computer and use it in GitHub Desktop.
Hack for Angular.js to inject authentication headers based on url and request body (if needed).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It works for v1.1.5. | |
// It's very fragile, so be careful using it with other versions. | |
// | |
// To understand how it works, check this out: https://github.com/angular/angular.js/blob/master/src/ng/httpBackend.js#L26-L31 | |
// | |
// Tried and failed more elegant solutions: | |
// - Interceptors. Couldn't get the request body. | |
// - `transformRequest` for the resource or $http. Couldn't get the body or the URL. | |
app.config(function ($httpBackendProvider) { | |
originalBackend = $httpBackendProvider.$get[3]; | |
newBackend = function (a,b,c) { | |
var originalBackendfunc = originalBackend(a,b,c); | |
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) { | |
// Do your changes here | |
originalBackendfunc(method, url, post, callback, headers, timeout, withCredentials, responseType); | |
}; | |
}; | |
$httpBackendProvider.$get[3] = newBackend | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment