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