Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c164df

Browse files
committed
Merge pull request witoldsz#10 from qloo/master
Added the ability to ignore URLs
2 parents 5638403 + be6d29c commit 2c164df

File tree

1 file changed

+70
-21
lines changed

1 file changed

+70
-21
lines changed

src/angular-http-auth.js

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,127 @@
1+
/*global angular:true, browser:true */
2+
13
/**
24
* @license HTTP Auth Interceptor Module for AngularJS
35
* (c) 2012 Witold Szczerba
46
* License: MIT
57
*/
68
angular.module('http-auth-interceptor', [])
79

8-
.provider('authService', function() {
10+
.provider('authService', function () {
911
/**
1012
* Holds all the requests which failed due to 401 response,
1113
* so they can be re-requested in future, once login is completed.
1214
*/
1315
var buffer = [];
14-
16+
17+
/**
18+
* Holds a list of functions that define rules for ignoring
19+
* the addition of requests to the buffer.
20+
*/
21+
var ignoreUrlExpressions = [];
22+
23+
/**
24+
* Adds functions to the `ignoreUrlExpressions` array.
25+
* The fn function takes a URL as a response as an argument and returns
26+
* `true` (to ignore the URL) or `false` (to allow the URL). When `true` is
27+
* returned no other expressions will be tested.
28+
*/
29+
this.addIgnoreUrlExpression = function (fn) {
30+
if (angular.isFunction(fn)) { ignoreUrlExpressions.push(fn); }
31+
return this;
32+
};
33+
34+
/**
35+
* Executes each of the ignore expressions to determine whether the URL
36+
* should be ignored.
37+
*
38+
* Example:
39+
*
40+
* angular.module('mod', ['http-auth-interceptor'])
41+
* .config(function (authServiceProvider) {
42+
* authServiceProvider.addIgnoreUrlExpression(function (response) {
43+
* return response.config.url === "/api/auth";
44+
* });
45+
* });
46+
*/
47+
this.shouldIgnoreUrl = function (response) {
48+
var fn, i, j = ignoreUrlExpressions.length;
49+
50+
for (i = 0; i < j; i++) {
51+
fn = ignoreUrlExpressions[i];
52+
if (fn(response) === true) { return true; }
53+
}
54+
55+
return false;
56+
};
57+
1558
/**
1659
* Required by HTTP interceptor.
1760
* Function is attached to provider to be invisible for regular users of this service.
1861
*/
19-
this.pushToBuffer = function(config, deferred) {
62+
this.pushToBuffer = function (config, deferred) {
2063
buffer.push({
21-
config: config,
64+
config: config,
2265
deferred: deferred
2366
});
24-
}
25-
26-
this.$get = ['$rootScope','$injector', function($rootScope, $injector) {
67+
};
68+
69+
this.$get = ['$rootScope', '$injector', function ($rootScope, $injector) {
2770
var $http; //initialized later because of circular dependency problem
2871
function retry(config, deferred) {
2972
$http = $http || $injector.get('$http');
30-
$http(config).then(function(response) {
73+
$http(config).then(function (response) {
3174
deferred.resolve(response);
3275
});
3376
}
3477
function retryAll() {
35-
for (var i = 0; i < buffer.length; ++i) {
78+
var i;
79+
80+
for (i = 0; i < buffer.length; ++i) {
3681
retry(buffer[i].config, buffer[i].deferred);
3782
}
3883
buffer = [];
3984
}
4085

4186
return {
42-
loginConfirmed: function() {
87+
loginConfirmed: function () {
4388
$rootScope.$broadcast('event:auth-loginConfirmed');
4489
retryAll();
4590
}
46-
}
47-
}]
91+
};
92+
}];
4893
})
4994

5095
/**
5196
* $http interceptor.
5297
* On 401 response - it stores the request and broadcasts 'event:angular-auth-loginRequired'.
5398
*/
54-
.config(['$httpProvider', 'authServiceProvider', function($httpProvider, authServiceProvider) {
55-
56-
var interceptor = ['$rootScope', '$q', function($rootScope, $q) {
99+
.config(['$httpProvider', 'authServiceProvider', function ($httpProvider, authServiceProvider) {
100+
101+
var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
57102
function success(response) {
58103
return response;
59104
}
60-
105+
61106
function error(response) {
62107
if (response.status === 401) {
63108
var deferred = $q.defer();
64-
authServiceProvider.pushToBuffer(response.config, deferred);
109+
110+
if (!authServiceProvider.shouldIgnoreUrl(response)) {
111+
authServiceProvider.pushToBuffer(response.config, deferred);
112+
}
113+
65114
$rootScope.$broadcast('event:auth-loginRequired');
66115
return deferred.promise;
67116
}
68117
// otherwise
69118
return $q.reject(response);
70119
}
71-
72-
return function(promise) {
120+
121+
return function (promise) {
73122
return promise.then(success, error);
74-
}
75-
123+
};
124+
76125
}];
77126
$httpProvider.responseInterceptors.push(interceptor);
78127
}]);

0 commit comments

Comments
 (0)