From 1e26e442e8b6d2d9e92b29dae1f0abb38adb1af8 Mon Sep 17 00:00:00 2001 From: Witold Szczerba Date: Wed, 23 Mar 2016 00:50:48 +0100 Subject: [PATCH 01/14] prevent repeating `event:auth-loginRequired` #95 #120 #130 --- src/http-auth-interceptor.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/http-auth-interceptor.js b/src/http-auth-interceptor.js index c2e62ed..0fd9789 100644 --- a/src/http-auth-interceptor.js +++ b/src/http-auth-interceptor.js @@ -56,8 +56,9 @@ switch (rejection.status) { case 401: var deferred = $q.defer(); - httpBuffer.append(config, deferred); - $rootScope.$broadcast('event:auth-loginRequired', rejection); + var bufferLength = httpBuffer.append(config, deferred); + if (bufferLength === 1) + $rootScope.$broadcast('event:auth-loginRequired', rejection); return deferred.promise; case 403: $rootScope.$broadcast('event:auth-forbidden', rejection); @@ -97,9 +98,10 @@ return { /** * Appends HTTP request configuration object with deferred response attached to buffer. + * @return {Number} The new length of the buffer. */ append: function(config, deferred) { - buffer.push({ + return buffer.push({ config: config, deferred: deferred }); From 10914bf218f436ce2de14eec6b3b319209b3aa4b Mon Sep 17 00:00:00 2001 From: Witold Szczerba Date: Fri, 25 Mar 2016 00:39:04 +0100 Subject: [PATCH 02/14] 1.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7171764..1fc46cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-http-auth", - "version": "1.2.3", + "version": "1.3.0", "description": "HTTP Auth Interceptor Module for AngularJS.", "main": "src/http-auth-interceptor.js", "repository": { From 55aa8512ea716df36445a8231010d5e33f6a2adc Mon Sep 17 00:00:00 2001 From: John Wnek Date: Sat, 9 Apr 2016 12:51:50 -0400 Subject: [PATCH 03/14] adding loginCancelled documentation to README --- README.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b62af87..52fdb11 100644 --- a/README.md +++ b/README.md @@ -30,13 +30,19 @@ the configuration object (this is the requested URL, payload and parameters) of every HTTP 401 response is buffered and everytime it happens, the `event:auth-loginRequired` message is broadcasted from $rootScope. -The `authService` has only one method: `loginConfirmed()`. -You are responsible to invoke this method after user logged in. You may optionally pass in -a data argument to the loginConfirmed method which will be passed on to the loginConfirmed +The `authService` has only 2 methods: `loginConfirmed()` and `loginCancelled()`. + +You are responsible to invoke `loginConfirmed()` after user logs in. You may optionally pass in +a data argument to this method which will be passed on to the loginConfirmed $broadcast. This may be useful, for example if you need to pass through details of the user that was logged in. The `authService` will then retry all the requests previously failed due to HTTP 401 response. +You are responsible to invoke `loginCancelled()` when authentication has been invalidated. You may optionally pass in +a data argument to this method which will be passed on to the loginCancelled +$broadcast. The `authService` will cancel all pending requests previously failed and buffered due +to HTTP 401 response. + In the event that a requested resource returns an HTTP 403 response (i.e. the user is authenticated but not authorized to access the resource), the user's request is discarded and the `event:auth-forbidden` message is broadcast from $rootScope. @@ -60,14 +66,19 @@ the `function(response) {do-something-with-response}` will fire, ###Advanced use case: ####Sending data to listeners: -You can supply additional data to observers across your application who are listening for `event:auth-loginConfirmed`: +You can supply additional data to observers across your application who are listening for `event:auth-loginConfirmed` and `event:auth-loginCancelled`: $scope.$on('event:auth-loginConfirmed', function(event, data){ $rootScope.isLoggedin = true; $log.log(data) }); -Use the `authService.loginConfirmed([data])` method to emit data with your login event. + $scope.$on('event:auth-loginCancelled', function(event, data){ + $rootScope.isLoggedin = false; + $log.log(data) + }); + +Use the `authService.loginConfirmed([data])` and `authService.loginCancelled([data])` methods to emit data with your login and logout events. ####Updating [$http(config)](https://docs.angularjs.org/api/ng/service/$http): Successful login means that the previous request are ready to be fired again, however now that login has occurred certain aspects of the previous requests might need to be modified on the fly. This is particularly important in a token based authentication scheme where an authorization token should be added to the header. From 5437350dbe412e7b039aacd1231a16a7aba27e1c Mon Sep 17 00:00:00 2001 From: Stephen Pengilley Date: Wed, 9 Nov 2016 15:20:53 +0000 Subject: [PATCH 04/14] Grunt uglification Minified js file --- .gitignore | 1 + Gruntfile.js | 23 +++++++++++++++++++++++ package.json | 8 +++++++- src/http-auth-interceptor.min.js | 1 + tasks/uglify.js | 20 ++++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Gruntfile.js create mode 100644 src/http-auth-interceptor.min.js create mode 100644 tasks/uglify.js diff --git a/.gitignore b/.gitignore index 522163c..67a0dad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules +npm-debug.log bower_components components diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..96d7d29 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,23 @@ +/** + * @license HTTP Auth Interceptor Module for AngularJS + * License: MIT + */ + +"use strict" + +module.exports = function(grunt) { + + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json') + }); + + grunt.loadTasks('tasks'); + + grunt.registerTask('default', function() { + grunt.log.ok("HTTP Auth Interceptor Module for AngularJS"); + grunt.log.ok("============================"); + grunt.task.run(["uglify"]); + grunt.log.ok("Minified ok"); + }); + +}; diff --git a/package.json b/package.json index 1fc46cb..d8781e7 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,11 @@ "angular", "auth" ], - "homepage": "https://github.com/witoldsz/angular-http-auth" + "homepage": "https://github.com/witoldsz/angular-http-auth", + "devDependencies": { + "grunt": "^0.4.5", + "grunt-contrib-uglify": "^0.11.0", + "load-grunt-config": "^0.19.1", + "time-grunt": "^1.3.0" + } } diff --git a/src/http-auth-interceptor.min.js b/src/http-auth-interceptor.min.js new file mode 100644 index 0000000..d3de029 --- /dev/null +++ b/src/http-auth-interceptor.min.js @@ -0,0 +1 @@ +/* angular-http-auth - 1.3.0 / Witold Szczerba */!function(){"use strict";angular.module("http-auth-interceptor",["http-auth-interceptor-buffer"]).factory("authService",["$rootScope","httpBuffer",function($rootScope,httpBuffer){return{loginConfirmed:function(data,configUpdater){var updater=configUpdater||function(config){return config};$rootScope.$broadcast("event:auth-loginConfirmed",data),httpBuffer.retryAll(updater)},loginCancelled:function(data,reason){httpBuffer.rejectAll(reason),$rootScope.$broadcast("event:auth-loginCancelled",data)}}}]).config(["$httpProvider",function($httpProvider){$httpProvider.interceptors.push(["$rootScope","$q","httpBuffer",function($rootScope,$q,httpBuffer){return{responseError:function(rejection){var config=rejection.config||{};if(!config.ignoreAuthModule)switch(rejection.status){case 401:var deferred=$q.defer(),bufferLength=httpBuffer.append(config,deferred);return 1===bufferLength&&$rootScope.$broadcast("event:auth-loginRequired",rejection),deferred.promise;case 403:$rootScope.$broadcast("event:auth-forbidden",rejection)}return $q.reject(rejection)}}}])}]),angular.module("http-auth-interceptor-buffer",[]).factory("httpBuffer",["$injector",function($injector){function retryHttpRequest(config,deferred){function successCallback(response){deferred.resolve(response)}function errorCallback(response){deferred.reject(response)}$http=$http||$injector.get("$http"),$http(config).then(successCallback,errorCallback)}var $http,buffer=[];return{append:function(config,deferred){return buffer.push({config:config,deferred:deferred})},rejectAll:function(reason){if(reason)for(var i=0;i - <%= pkg.version %> / <%= pkg.author %> */" + }, + src: ['**/*.js', '!**/*.min.js'], + cwd: 'src', + dest: 'src', + expand: true, + ext: '.min.js', + } + }); + + grunt.loadNpmTasks('grunt-contrib-uglify'); +}; From e6d8191dc8406d663dae8b075ceca36a06e400bf Mon Sep 17 00:00:00 2001 From: Stephen Pengilley Date: Thu, 10 Nov 2016 10:11:43 +0000 Subject: [PATCH 05/14] More streamline solution to this problem --- Gruntfile.js | 23 ----------------------- package.json | 13 +++++++------ src/http-auth-interceptor.min.js | 1 - tasks/uglify.js | 20 -------------------- 4 files changed, 7 insertions(+), 50 deletions(-) delete mode 100644 Gruntfile.js delete mode 100644 src/http-auth-interceptor.min.js delete mode 100644 tasks/uglify.js diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 96d7d29..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @license HTTP Auth Interceptor Module for AngularJS - * License: MIT - */ - -"use strict" - -module.exports = function(grunt) { - - grunt.initConfig({ - pkg: grunt.file.readJSON('package.json') - }); - - grunt.loadTasks('tasks'); - - grunt.registerTask('default', function() { - grunt.log.ok("HTTP Auth Interceptor Module for AngularJS"); - grunt.log.ok("============================"); - grunt.task.run(["uglify"]); - grunt.log.ok("Minified ok"); - }); - -}; diff --git a/package.json b/package.json index d8781e7..8d3db61 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,11 @@ "auth" ], "homepage": "https://github.com/witoldsz/angular-http-auth", - "devDependencies": { - "grunt": "^0.4.5", - "grunt-contrib-uglify": "^0.11.0", - "load-grunt-config": "^0.19.1", - "time-grunt": "^1.3.0" - } + "devDependencies": { + "mkdirp": "^0.5.1", + "uglifyjs": "^2.4.10" + }, + "scripts": { + "minify": "mkdirp dist && uglifyjs src/http-auth-interceptor.js -o dist/http-auth-interceptor.min.js -c" + } } diff --git a/src/http-auth-interceptor.min.js b/src/http-auth-interceptor.min.js deleted file mode 100644 index d3de029..0000000 --- a/src/http-auth-interceptor.min.js +++ /dev/null @@ -1 +0,0 @@ -/* angular-http-auth - 1.3.0 / Witold Szczerba */!function(){"use strict";angular.module("http-auth-interceptor",["http-auth-interceptor-buffer"]).factory("authService",["$rootScope","httpBuffer",function($rootScope,httpBuffer){return{loginConfirmed:function(data,configUpdater){var updater=configUpdater||function(config){return config};$rootScope.$broadcast("event:auth-loginConfirmed",data),httpBuffer.retryAll(updater)},loginCancelled:function(data,reason){httpBuffer.rejectAll(reason),$rootScope.$broadcast("event:auth-loginCancelled",data)}}}]).config(["$httpProvider",function($httpProvider){$httpProvider.interceptors.push(["$rootScope","$q","httpBuffer",function($rootScope,$q,httpBuffer){return{responseError:function(rejection){var config=rejection.config||{};if(!config.ignoreAuthModule)switch(rejection.status){case 401:var deferred=$q.defer(),bufferLength=httpBuffer.append(config,deferred);return 1===bufferLength&&$rootScope.$broadcast("event:auth-loginRequired",rejection),deferred.promise;case 403:$rootScope.$broadcast("event:auth-forbidden",rejection)}return $q.reject(rejection)}}}])}]),angular.module("http-auth-interceptor-buffer",[]).factory("httpBuffer",["$injector",function($injector){function retryHttpRequest(config,deferred){function successCallback(response){deferred.resolve(response)}function errorCallback(response){deferred.reject(response)}$http=$http||$injector.get("$http"),$http(config).then(successCallback,errorCallback)}var $http,buffer=[];return{append:function(config,deferred){return buffer.push({config:config,deferred:deferred})},rejectAll:function(reason){if(reason)for(var i=0;i - <%= pkg.version %> / <%= pkg.author %> */" - }, - src: ['**/*.js', '!**/*.min.js'], - cwd: 'src', - dest: 'src', - expand: true, - ext: '.min.js', - } - }); - - grunt.loadNpmTasks('grunt-contrib-uglify'); -}; From 6458076c8da6fa1c6d78d55add2e2dbb2c2bf9c2 Mon Sep 17 00:00:00 2001 From: Witold Szczerba Date: Sat, 12 Nov 2016 00:24:41 +0100 Subject: [PATCH 06/14] package.json scripts --- package.json | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8d3db61..dcf467c 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,18 @@ "homepage": "https://github.com/witoldsz/angular-http-auth", "devDependencies": { "mkdirp": "^0.5.1", + "rimraf": "^2.5.4", "uglifyjs": "^2.4.10" }, "scripts": { - "minify": "mkdirp dist && uglifyjs src/http-auth-interceptor.js -o dist/http-auth-interceptor.min.js -c" + "clean": "rimraf dist/*", + + "prebuild": "npm run clean -s && mkdirp dist", + "build": "npm run build:minify -s && npm run build:copy -s", + "build:minify": "uglifyjs src/http-auth-interceptor.js -o dist/http-auth-interceptor.min.js -c", + "build:copy": "cat src/http-auth-interceptor.js > dist/http-auth-interceptor.js", + + "version": "npm run build && git add -A dist", + "postversion": "git push && git push --tags" } } From afec65586d9e242f818c10a37aed2c724795124e Mon Sep 17 00:00:00 2001 From: Witold Szczerba Date: Sat, 12 Nov 2016 00:26:47 +0100 Subject: [PATCH 07/14] 1.4.0 --- dist/http-auth-interceptor.js | 135 ++++++++++++++++++++++++++++++ dist/http-auth-interceptor.min.js | 1 + package.json | 4 +- 3 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 dist/http-auth-interceptor.js create mode 100644 dist/http-auth-interceptor.min.js diff --git a/dist/http-auth-interceptor.js b/dist/http-auth-interceptor.js new file mode 100644 index 0000000..0fd9789 --- /dev/null +++ b/dist/http-auth-interceptor.js @@ -0,0 +1,135 @@ +/*global angular:true, browser:true */ + +/** + * @license HTTP Auth Interceptor Module for AngularJS + * (c) 2012 Witold Szczerba + * License: MIT + */ +(function () { + 'use strict'; + + angular.module('http-auth-interceptor', ['http-auth-interceptor-buffer']) + + .factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) { + return { + /** + * Call this function to indicate that authentication was successfull and trigger a + * retry of all deferred requests. + * @param data an optional argument to pass on to $broadcast which may be useful for + * example if you need to pass through details of the user that was logged in + * @param configUpdater an optional transformation function that can modify the + * requests that are retried after having logged in. This can be used for example + * to add an authentication token. It must return the request. + */ + loginConfirmed: function(data, configUpdater) { + var updater = configUpdater || function(config) {return config;}; + $rootScope.$broadcast('event:auth-loginConfirmed', data); + httpBuffer.retryAll(updater); + }, + + /** + * Call this function to indicate that authentication should not proceed. + * All deferred requests will be abandoned or rejected (if reason is provided). + * @param data an optional argument to pass on to $broadcast. + * @param reason if provided, the requests are rejected; abandoned otherwise. + */ + loginCancelled: function(data, reason) { + httpBuffer.rejectAll(reason); + $rootScope.$broadcast('event:auth-loginCancelled', data); + } + }; + }]) + + /** + * $http interceptor. + * On 401 response (without 'ignoreAuthModule' option) stores the request + * and broadcasts 'event:auth-loginRequired'. + * On 403 response (without 'ignoreAuthModule' option) discards the request + * and broadcasts 'event:auth-forbidden'. + */ + .config(['$httpProvider', function($httpProvider) { + $httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) { + return { + responseError: function(rejection) { + var config = rejection.config || {}; + if (!config.ignoreAuthModule) { + switch (rejection.status) { + case 401: + var deferred = $q.defer(); + var bufferLength = httpBuffer.append(config, deferred); + if (bufferLength === 1) + $rootScope.$broadcast('event:auth-loginRequired', rejection); + return deferred.promise; + case 403: + $rootScope.$broadcast('event:auth-forbidden', rejection); + break; + } + } + // otherwise, default behaviour + return $q.reject(rejection); + } + }; + }]); + }]); + + /** + * Private module, a utility, required internally by 'http-auth-interceptor'. + */ + angular.module('http-auth-interceptor-buffer', []) + + .factory('httpBuffer', ['$injector', function($injector) { + /** Holds all the requests, so they can be re-requested in future. */ + var buffer = []; + + /** Service initialized later because of circular dependency problem. */ + var $http; + + function retryHttpRequest(config, deferred) { + function successCallback(response) { + deferred.resolve(response); + } + function errorCallback(response) { + deferred.reject(response); + } + $http = $http || $injector.get('$http'); + $http(config).then(successCallback, errorCallback); + } + + return { + /** + * Appends HTTP request configuration object with deferred response attached to buffer. + * @return {Number} The new length of the buffer. + */ + append: function(config, deferred) { + return buffer.push({ + config: config, + deferred: deferred + }); + }, + + /** + * Abandon or reject (if reason provided) all the buffered requests. + */ + rejectAll: function(reason) { + if (reason) { + for (var i = 0; i < buffer.length; ++i) { + buffer[i].deferred.reject(reason); + } + } + buffer = []; + }, + + /** + * Retries all the buffered requests clears the buffer. + */ + retryAll: function(updater) { + for (var i = 0; i < buffer.length; ++i) { + var _cfg = updater(buffer[i].config); + if (_cfg !== false) + retryHttpRequest(_cfg, buffer[i].deferred); + } + buffer = []; + } + }; + }]); +})(); diff --git a/dist/http-auth-interceptor.min.js b/dist/http-auth-interceptor.min.js new file mode 100644 index 0000000..726bbed --- /dev/null +++ b/dist/http-auth-interceptor.min.js @@ -0,0 +1 @@ +!function(){"use strict";angular.module("http-auth-interceptor",["http-auth-interceptor-buffer"]).factory("authService",["$rootScope","httpBuffer",function($rootScope,httpBuffer){return{loginConfirmed:function(data,configUpdater){var updater=configUpdater||function(config){return config};$rootScope.$broadcast("event:auth-loginConfirmed",data),httpBuffer.retryAll(updater)},loginCancelled:function(data,reason){httpBuffer.rejectAll(reason),$rootScope.$broadcast("event:auth-loginCancelled",data)}}}]).config(["$httpProvider",function($httpProvider){$httpProvider.interceptors.push(["$rootScope","$q","httpBuffer",function($rootScope,$q,httpBuffer){return{responseError:function(rejection){var config=rejection.config||{};if(!config.ignoreAuthModule)switch(rejection.status){case 401:var deferred=$q.defer(),bufferLength=httpBuffer.append(config,deferred);return 1===bufferLength&&$rootScope.$broadcast("event:auth-loginRequired",rejection),deferred.promise;case 403:$rootScope.$broadcast("event:auth-forbidden",rejection)}return $q.reject(rejection)}}}])}]),angular.module("http-auth-interceptor-buffer",[]).factory("httpBuffer",["$injector",function($injector){function retryHttpRequest(config,deferred){function successCallback(response){deferred.resolve(response)}function errorCallback(response){deferred.reject(response)}$http=$http||$injector.get("$http"),$http(config).then(successCallback,errorCallback)}var $http,buffer=[];return{append:function(config,deferred){return buffer.push({config:config,deferred:deferred})},rejectAll:function(reason){if(reason)for(var i=0;i dist/http-auth-interceptor.js", - "version": "npm run build && git add -A dist", "postversion": "git push && git push --tags" } From 39e0a059bc32fa612e3822072b4c06f08f40a2ea Mon Sep 17 00:00:00 2001 From: Mikael Korpela Date: Sat, 12 Nov 2016 12:08:42 +0200 Subject: [PATCH 08/14] Add license key to bower.json #137 --- bower.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 27daaaf..54fac45 100644 --- a/bower.json +++ b/bower.json @@ -12,5 +12,6 @@ }, "ignore": [ "package.json" - ] + ], + "license": "MIT" } From af257667c273333cca377cc4688aff42c9174afc Mon Sep 17 00:00:00 2001 From: Witold Szczerba Date: Sat, 12 Nov 2016 13:13:46 +0100 Subject: [PATCH 09/14] 1.4.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ef56b2e..30affc7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-http-auth", - "version": "1.4.0", + "version": "1.4.1", "description": "HTTP Auth Interceptor Module for AngularJS.", "main": "src/http-auth-interceptor.js", "repository": { From 86e94f6a1c9f1adc92f9089779926b4a45343fdf Mon Sep 17 00:00:00 2001 From: Michael Fladischer Date: Tue, 23 Feb 2016 20:01:06 +0100 Subject: [PATCH 10/14] Add support for CommonJS loading. Now that angular has CommonJS support it would be nice to have this too. --- src/http-auth-interceptor.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/http-auth-interceptor.js b/src/http-auth-interceptor.js index 0fd9789..060d8f8 100644 --- a/src/http-auth-interceptor.js +++ b/src/http-auth-interceptor.js @@ -5,6 +5,7 @@ * (c) 2012 Witold Szczerba * License: MIT */ + (function () { 'use strict'; @@ -133,3 +134,8 @@ }; }]); })(); + +/* commonjs package manager support (eg componentjs) */ +if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){ + module.exports = 'http-auth-interceptor'; +} From fc131b5a0d122c31e2bd77edd28a39b2fac5c1d5 Mon Sep 17 00:00:00 2001 From: Witold Szczerba Date: Thu, 17 Nov 2016 16:20:51 +0100 Subject: [PATCH 11/14] 1.5.0 --- dist/http-auth-interceptor.js | 6 ++++++ dist/http-auth-interceptor.min.js | 2 +- package.json | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dist/http-auth-interceptor.js b/dist/http-auth-interceptor.js index 0fd9789..060d8f8 100644 --- a/dist/http-auth-interceptor.js +++ b/dist/http-auth-interceptor.js @@ -5,6 +5,7 @@ * (c) 2012 Witold Szczerba * License: MIT */ + (function () { 'use strict'; @@ -133,3 +134,8 @@ }; }]); })(); + +/* commonjs package manager support (eg componentjs) */ +if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){ + module.exports = 'http-auth-interceptor'; +} diff --git a/dist/http-auth-interceptor.min.js b/dist/http-auth-interceptor.min.js index 726bbed..167529a 100644 --- a/dist/http-auth-interceptor.min.js +++ b/dist/http-auth-interceptor.min.js @@ -1 +1 @@ -!function(){"use strict";angular.module("http-auth-interceptor",["http-auth-interceptor-buffer"]).factory("authService",["$rootScope","httpBuffer",function($rootScope,httpBuffer){return{loginConfirmed:function(data,configUpdater){var updater=configUpdater||function(config){return config};$rootScope.$broadcast("event:auth-loginConfirmed",data),httpBuffer.retryAll(updater)},loginCancelled:function(data,reason){httpBuffer.rejectAll(reason),$rootScope.$broadcast("event:auth-loginCancelled",data)}}}]).config(["$httpProvider",function($httpProvider){$httpProvider.interceptors.push(["$rootScope","$q","httpBuffer",function($rootScope,$q,httpBuffer){return{responseError:function(rejection){var config=rejection.config||{};if(!config.ignoreAuthModule)switch(rejection.status){case 401:var deferred=$q.defer(),bufferLength=httpBuffer.append(config,deferred);return 1===bufferLength&&$rootScope.$broadcast("event:auth-loginRequired",rejection),deferred.promise;case 403:$rootScope.$broadcast("event:auth-forbidden",rejection)}return $q.reject(rejection)}}}])}]),angular.module("http-auth-interceptor-buffer",[]).factory("httpBuffer",["$injector",function($injector){function retryHttpRequest(config,deferred){function successCallback(response){deferred.resolve(response)}function errorCallback(response){deferred.reject(response)}$http=$http||$injector.get("$http"),$http(config).then(successCallback,errorCallback)}var $http,buffer=[];return{append:function(config,deferred){return buffer.push({config:config,deferred:deferred})},rejectAll:function(reason){if(reason)for(var i=0;i Date: Sun, 5 Mar 2017 19:24:38 +0530 Subject: [PATCH 12/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 52fdb11..15bf2bc 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The `loginConfirmed` method supports the injection of an Updater function that w authService.loginConfirmed([data], [Updater-Function]) //application of tokens to previously fired requests: - var token = reponse.token; + var token = response.token; authService.loginConfirmed('success', function(config){ config.headers["Authorization"] = token; From cc316e352083d8e0f3e72e7171a1e41743fabf4c Mon Sep 17 00:00:00 2001 From: Peter Neave Date: Tue, 21 Mar 2017 09:20:13 +1100 Subject: [PATCH 13/14] Fix minor spelling mistake --- dist/http-auth-interceptor.js | 2 +- src/http-auth-interceptor.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/http-auth-interceptor.js b/dist/http-auth-interceptor.js index 060d8f8..3cff6cc 100644 --- a/dist/http-auth-interceptor.js +++ b/dist/http-auth-interceptor.js @@ -14,7 +14,7 @@ .factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) { return { /** - * Call this function to indicate that authentication was successfull and trigger a + * Call this function to indicate that authentication was successful and trigger a * retry of all deferred requests. * @param data an optional argument to pass on to $broadcast which may be useful for * example if you need to pass through details of the user that was logged in diff --git a/src/http-auth-interceptor.js b/src/http-auth-interceptor.js index 060d8f8..3cff6cc 100644 --- a/src/http-auth-interceptor.js +++ b/src/http-auth-interceptor.js @@ -14,7 +14,7 @@ .factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) { return { /** - * Call this function to indicate that authentication was successfull and trigger a + * Call this function to indicate that authentication was successful and trigger a * retry of all deferred requests. * @param data an optional argument to pass on to $broadcast which may be useful for * example if you need to pass through details of the user that was logged in From aa2456d3cd40a32c673b05dbcd0a6ee13c9ee4d7 Mon Sep 17 00:00:00 2001 From: Viktor Lieskovsky Date: Tue, 3 Oct 2017 17:51:56 +0200 Subject: [PATCH 14/14] fix formatting of README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 15bf2bc..ad36219 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ the `event:auth-forbidden` message is broadcast from $rootScope. Sometimes you might not want the interceptor to intercept a request even if one returns 401 or 403. In a case like this you can add `ignoreAuthModule: true` to the request config. A common use case for this would be, for example, a login request which returns 401 if the login credentials are invalid. -###Typical use case: +### Typical use case: * somewhere (some service or controller) the: `$http(...).then(function(response) { do-something-with-response })` is invoked, * the response of that requests is a **HTTP 401**, @@ -63,9 +63,9 @@ Sometimes you might not want the interceptor to intercept a request even if one the `function(response) {do-something-with-response}` will fire, * your application will continue as nothing had happened. -###Advanced use case: +### Advanced use case: -####Sending data to listeners: +#### Sending data to listeners: You can supply additional data to observers across your application who are listening for `event:auth-loginConfirmed` and `event:auth-loginCancelled`: $scope.$on('event:auth-loginConfirmed', function(event, data){ @@ -80,7 +80,7 @@ You can supply additional data to observers across your application who are list Use the `authService.loginConfirmed([data])` and `authService.loginCancelled([data])` methods to emit data with your login and logout events. -####Updating [$http(config)](https://docs.angularjs.org/api/ng/service/$http): +#### Updating [$http(config)](https://docs.angularjs.org/api/ng/service/$http): Successful login means that the previous request are ready to be fired again, however now that login has occurred certain aspects of the previous requests might need to be modified on the fly. This is particularly important in a token based authentication scheme where an authorization token should be added to the header. The `loginConfirmed` method supports the injection of an Updater function that will apply changes to the http config object.