Skip to content

Commit 79cf2bb

Browse files
committed
Merge pull request #79 from kriswillis/master
Added support for HTTP 403 responses
2 parents 07950ac + f99e8a6 commit 79cf2bb

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ $broadcast. This may be useful, for example if you need to pass through details
3030
that was logged in. The `authService` will then retry all the requests previously failed due
3131
to HTTP 401 response.
3232

33+
In the event that a requested resource returns an HTTP 403 response (i.e. the user is
34+
authenticated but not authorized to access the resource), the user's request is discarded and
35+
the `event:auth-forbidden` message is broadcasted from $rootScope.
36+
3337
###Typical use case:
3438

3539
* somewhere (some service or controller) the: `$http(...).then(function(response) { do-something-with-response })` is invoked,

Diff for: src/http-auth-interceptor.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,24 @@
4444
* $http interceptor.
4545
* On 401 response (without 'ignoreAuthModule' option) stores the request
4646
* and broadcasts 'event:auth-loginRequired'.
47+
* On 403 response (without 'ignoreAuthModule' option) discards the request
48+
* and broadcasts 'event:auth-forbidden'.
4749
*/
4850
.config(['$httpProvider', function($httpProvider) {
4951
$httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {
5052
return {
5153
responseError: function(rejection) {
52-
if (rejection.status === 401 && !rejection.config.ignoreAuthModule) {
53-
var deferred = $q.defer();
54-
httpBuffer.append(rejection.config, deferred);
55-
$rootScope.$broadcast('event:auth-loginRequired', rejection);
56-
return deferred.promise;
54+
if (!rejection.config.ignoreAuthModule) {
55+
switch (rejection.status) {
56+
case 401:
57+
var deferred = $q.defer();
58+
httpBuffer.append(rejection.config, deferred);
59+
$rootScope.$broadcast('event:auth-loginRequired', rejection);
60+
return deferred.promise;
61+
case 403:
62+
$rootScope.$broadcast('event:auth-forbidden', rejection);
63+
break;
64+
}
5765
}
5866
// otherwise, default behaviour
5967
return $q.reject(rejection);

0 commit comments

Comments
 (0)