Skip to content

Commit

Permalink
Merge pull request #79 from kriswillis/master
Browse files Browse the repository at this point in the history
Added support for HTTP 403 responses
  • Loading branch information
witoldsz committed Sep 16, 2014
2 parents 07950ac + f99e8a6 commit 79cf2bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ $broadcast. This may be useful, for example if you need to pass through details
that was logged in. The `authService` will then retry all the requests previously failed 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 broadcasted from $rootScope.

###Typical use case:

* somewhere (some service or controller) the: `$http(...).then(function(response) { do-something-with-response })` is invoked,
Expand Down
18 changes: 13 additions & 5 deletions src/http-auth-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,24 @@
* $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) {
if (rejection.status === 401 && !rejection.config.ignoreAuthModule) {
var deferred = $q.defer();
httpBuffer.append(rejection.config, deferred);
$rootScope.$broadcast('event:auth-loginRequired', rejection);
return deferred.promise;
if (!rejection.config.ignoreAuthModule) {
switch (rejection.status) {
case 401:
var deferred = $q.defer();
httpBuffer.append(rejection.config, deferred);
$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);
Expand Down

2 comments on commit 79cf2bb

@denisazevedo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excelent! Are you creating a release for this? Thanks

@witoldsz
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think I should, maybe this weekend...

Please sign in to comment.