Skip to content

Commit afec655

Browse files
committed
1.4.0
1 parent 6458076 commit afec655

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

dist/http-auth-interceptor.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*global angular:true, browser:true */
2+
3+
/**
4+
* @license HTTP Auth Interceptor Module for AngularJS
5+
* (c) 2012 Witold Szczerba
6+
* License: MIT
7+
*/
8+
(function () {
9+
'use strict';
10+
11+
angular.module('http-auth-interceptor', ['http-auth-interceptor-buffer'])
12+
13+
.factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) {
14+
return {
15+
/**
16+
* Call this function to indicate that authentication was successfull and trigger a
17+
* retry of all deferred requests.
18+
* @param data an optional argument to pass on to $broadcast which may be useful for
19+
* example if you need to pass through details of the user that was logged in
20+
* @param configUpdater an optional transformation function that can modify the
21+
* requests that are retried after having logged in. This can be used for example
22+
* to add an authentication token. It must return the request.
23+
*/
24+
loginConfirmed: function(data, configUpdater) {
25+
var updater = configUpdater || function(config) {return config;};
26+
$rootScope.$broadcast('event:auth-loginConfirmed', data);
27+
httpBuffer.retryAll(updater);
28+
},
29+
30+
/**
31+
* Call this function to indicate that authentication should not proceed.
32+
* All deferred requests will be abandoned or rejected (if reason is provided).
33+
* @param data an optional argument to pass on to $broadcast.
34+
* @param reason if provided, the requests are rejected; abandoned otherwise.
35+
*/
36+
loginCancelled: function(data, reason) {
37+
httpBuffer.rejectAll(reason);
38+
$rootScope.$broadcast('event:auth-loginCancelled', data);
39+
}
40+
};
41+
}])
42+
43+
/**
44+
* $http interceptor.
45+
* On 401 response (without 'ignoreAuthModule' option) stores the request
46+
* and broadcasts 'event:auth-loginRequired'.
47+
* On 403 response (without 'ignoreAuthModule' option) discards the request
48+
* and broadcasts 'event:auth-forbidden'.
49+
*/
50+
.config(['$httpProvider', function($httpProvider) {
51+
$httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {
52+
return {
53+
responseError: function(rejection) {
54+
var config = rejection.config || {};
55+
if (!config.ignoreAuthModule) {
56+
switch (rejection.status) {
57+
case 401:
58+
var deferred = $q.defer();
59+
var bufferLength = httpBuffer.append(config, deferred);
60+
if (bufferLength === 1)
61+
$rootScope.$broadcast('event:auth-loginRequired', rejection);
62+
return deferred.promise;
63+
case 403:
64+
$rootScope.$broadcast('event:auth-forbidden', rejection);
65+
break;
66+
}
67+
}
68+
// otherwise, default behaviour
69+
return $q.reject(rejection);
70+
}
71+
};
72+
}]);
73+
}]);
74+
75+
/**
76+
* Private module, a utility, required internally by 'http-auth-interceptor'.
77+
*/
78+
angular.module('http-auth-interceptor-buffer', [])
79+
80+
.factory('httpBuffer', ['$injector', function($injector) {
81+
/** Holds all the requests, so they can be re-requested in future. */
82+
var buffer = [];
83+
84+
/** Service initialized later because of circular dependency problem. */
85+
var $http;
86+
87+
function retryHttpRequest(config, deferred) {
88+
function successCallback(response) {
89+
deferred.resolve(response);
90+
}
91+
function errorCallback(response) {
92+
deferred.reject(response);
93+
}
94+
$http = $http || $injector.get('$http');
95+
$http(config).then(successCallback, errorCallback);
96+
}
97+
98+
return {
99+
/**
100+
* Appends HTTP request configuration object with deferred response attached to buffer.
101+
* @return {Number} The new length of the buffer.
102+
*/
103+
append: function(config, deferred) {
104+
return buffer.push({
105+
config: config,
106+
deferred: deferred
107+
});
108+
},
109+
110+
/**
111+
* Abandon or reject (if reason provided) all the buffered requests.
112+
*/
113+
rejectAll: function(reason) {
114+
if (reason) {
115+
for (var i = 0; i < buffer.length; ++i) {
116+
buffer[i].deferred.reject(reason);
117+
}
118+
}
119+
buffer = [];
120+
},
121+
122+
/**
123+
* Retries all the buffered requests clears the buffer.
124+
*/
125+
retryAll: function(updater) {
126+
for (var i = 0; i < buffer.length; ++i) {
127+
var _cfg = updater(buffer[i].config);
128+
if (_cfg !== false)
129+
retryHttpRequest(_cfg, buffer[i].deferred);
130+
}
131+
buffer = [];
132+
}
133+
};
134+
}]);
135+
})();

dist/http-auth-interceptor.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-http-auth",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "HTTP Auth Interceptor Module for AngularJS.",
55
"main": "src/http-auth-interceptor.js",
66
"repository": {
@@ -24,12 +24,10 @@
2424
},
2525
"scripts": {
2626
"clean": "rimraf dist/*",
27-
2827
"prebuild": "npm run clean -s && mkdirp dist",
2928
"build": "npm run build:minify -s && npm run build:copy -s",
3029
"build:minify": "uglifyjs src/http-auth-interceptor.js -o dist/http-auth-interceptor.min.js -c",
3130
"build:copy": "cat src/http-auth-interceptor.js > dist/http-auth-interceptor.js",
32-
3331
"version": "npm run build && git add -A dist",
3432
"postversion": "git push && git push --tags"
3533
}

0 commit comments

Comments
 (0)