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

Skip to content

Commit 01fa78f

Browse files
committed
Add the ability to prevent URLs from being added to the buffer
1 parent 5638403 commit 01fa78f

File tree

1 file changed

+60
-21
lines changed

1 file changed

+60
-21
lines changed

src/angular-http-auth.js

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,117 @@
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+
};
32+
33+
/**
34+
* Executes each of the ignore expressions to determine whether the URL
35+
* should be ignored.
36+
*/
37+
this.shouldIgnoreUrl = function (response) {
38+
var fn, i, j = ignoreUrlExpressions.length;
39+
40+
for (i = 0; i < j; i++) {
41+
fn = ignoreUrlExpressions[i];
42+
if (fn(response) === true) { return true; }
43+
}
44+
45+
return false;
46+
};
47+
1548
/**
1649
* Required by HTTP interceptor.
1750
* Function is attached to provider to be invisible for regular users of this service.
1851
*/
19-
this.pushToBuffer = function(config, deferred) {
52+
this.pushToBuffer = function (config, deferred) {
2053
buffer.push({
21-
config: config,
54+
config: config,
2255
deferred: deferred
2356
});
24-
}
25-
26-
this.$get = ['$rootScope','$injector', function($rootScope, $injector) {
57+
};
58+
59+
this.$get = ['$rootScope', '$injector', function ($rootScope, $injector) {
2760
var $http; //initialized later because of circular dependency problem
2861
function retry(config, deferred) {
2962
$http = $http || $injector.get('$http');
30-
$http(config).then(function(response) {
63+
$http(config).then(function (response) {
3164
deferred.resolve(response);
3265
});
3366
}
3467
function retryAll() {
35-
for (var i = 0; i < buffer.length; ++i) {
68+
var i;
69+
70+
for (i = 0; i < buffer.length; ++i) {
3671
retry(buffer[i].config, buffer[i].deferred);
3772
}
3873
buffer = [];
3974
}
4075

4176
return {
42-
loginConfirmed: function() {
77+
loginConfirmed: function () {
4378
$rootScope.$broadcast('event:auth-loginConfirmed');
4479
retryAll();
4580
}
46-
}
47-
}]
81+
};
82+
}];
4883
})
4984

5085
/**
5186
* $http interceptor.
5287
* On 401 response - it stores the request and broadcasts 'event:angular-auth-loginRequired'.
5388
*/
54-
.config(['$httpProvider', 'authServiceProvider', function($httpProvider, authServiceProvider) {
55-
56-
var interceptor = ['$rootScope', '$q', function($rootScope, $q) {
89+
.config(['$httpProvider', 'authServiceProvider', function ($httpProvider, authServiceProvider) {
90+
91+
var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
5792
function success(response) {
5893
return response;
5994
}
60-
95+
6196
function error(response) {
6297
if (response.status === 401) {
6398
var deferred = $q.defer();
64-
authServiceProvider.pushToBuffer(response.config, deferred);
99+
100+
if (!authServiceProvider.shouldIgnoreUrl(response)) {
101+
authServiceProvider.pushToBuffer(response.config, deferred);
102+
}
103+
65104
$rootScope.$broadcast('event:auth-loginRequired');
66105
return deferred.promise;
67106
}
68107
// otherwise
69108
return $q.reject(response);
70109
}
71-
72-
return function(promise) {
110+
111+
return function (promise) {
73112
return promise.then(success, error);
74-
}
75-
113+
};
114+
76115
}];
77116
$httpProvider.responseInterceptors.push(interceptor);
78117
}]);

0 commit comments

Comments
 (0)