|
| 1 | +/*global angular:true, browser:true */ |
| 2 | + |
1 | 3 | /**
|
2 | 4 | * @license HTTP Auth Interceptor Module for AngularJS
|
3 | 5 | * (c) 2012 Witold Szczerba
|
4 | 6 | * License: MIT
|
5 | 7 | */
|
6 | 8 | angular.module('http-auth-interceptor', [])
|
7 | 9 |
|
8 |
| - .provider('authService', function() { |
| 10 | + .provider('authService', function () { |
9 | 11 | /**
|
10 | 12 | * Holds all the requests which failed due to 401 response,
|
11 | 13 | * so they can be re-requested in future, once login is completed.
|
12 | 14 | */
|
13 | 15 | 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 | + |
15 | 48 | /**
|
16 | 49 | * Required by HTTP interceptor.
|
17 | 50 | * Function is attached to provider to be invisible for regular users of this service.
|
18 | 51 | */
|
19 |
| - this.pushToBuffer = function(config, deferred) { |
| 52 | + this.pushToBuffer = function (config, deferred) { |
20 | 53 | buffer.push({
|
21 |
| - config: config, |
| 54 | + config: config, |
22 | 55 | deferred: deferred
|
23 | 56 | });
|
24 |
| - } |
25 |
| - |
26 |
| - this.$get = ['$rootScope','$injector', function($rootScope, $injector) { |
| 57 | + }; |
| 58 | + |
| 59 | + this.$get = ['$rootScope', '$injector', function ($rootScope, $injector) { |
27 | 60 | var $http; //initialized later because of circular dependency problem
|
28 | 61 | function retry(config, deferred) {
|
29 | 62 | $http = $http || $injector.get('$http');
|
30 |
| - $http(config).then(function(response) { |
| 63 | + $http(config).then(function (response) { |
31 | 64 | deferred.resolve(response);
|
32 | 65 | });
|
33 | 66 | }
|
34 | 67 | function retryAll() {
|
35 |
| - for (var i = 0; i < buffer.length; ++i) { |
| 68 | + var i; |
| 69 | + |
| 70 | + for (i = 0; i < buffer.length; ++i) { |
36 | 71 | retry(buffer[i].config, buffer[i].deferred);
|
37 | 72 | }
|
38 | 73 | buffer = [];
|
39 | 74 | }
|
40 | 75 |
|
41 | 76 | return {
|
42 |
| - loginConfirmed: function() { |
| 77 | + loginConfirmed: function () { |
43 | 78 | $rootScope.$broadcast('event:auth-loginConfirmed');
|
44 | 79 | retryAll();
|
45 | 80 | }
|
46 |
| - } |
47 |
| - }] |
| 81 | + }; |
| 82 | + }]; |
48 | 83 | })
|
49 | 84 |
|
50 | 85 | /**
|
51 | 86 | * $http interceptor.
|
52 | 87 | * On 401 response - it stores the request and broadcasts 'event:angular-auth-loginRequired'.
|
53 | 88 | */
|
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) { |
57 | 92 | function success(response) {
|
58 | 93 | return response;
|
59 | 94 | }
|
60 |
| - |
| 95 | + |
61 | 96 | function error(response) {
|
62 | 97 | if (response.status === 401) {
|
63 | 98 | var deferred = $q.defer();
|
64 |
| - authServiceProvider.pushToBuffer(response.config, deferred); |
| 99 | + |
| 100 | + if (!authServiceProvider.shouldIgnoreUrl(response)) { |
| 101 | + authServiceProvider.pushToBuffer(response.config, deferred); |
| 102 | + } |
| 103 | + |
65 | 104 | $rootScope.$broadcast('event:auth-loginRequired');
|
66 | 105 | return deferred.promise;
|
67 | 106 | }
|
68 | 107 | // otherwise
|
69 | 108 | return $q.reject(response);
|
70 | 109 | }
|
71 |
| - |
72 |
| - return function(promise) { |
| 110 | + |
| 111 | + return function (promise) { |
73 | 112 | return promise.then(success, error);
|
74 |
| - } |
75 |
| - |
| 113 | + }; |
| 114 | + |
76 | 115 | }];
|
77 | 116 | $httpProvider.responseInterceptors.push(interceptor);
|
78 | 117 | }]);
|
0 commit comments