This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #796 from trainerbill/UnauthorizedRoute2
Unauthorized client side routing
- Loading branch information
Showing
6 changed files
with
126 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
modules/core/client/services/interceptors/auth.interceptor.client.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
angular.module('core').factory('authInterceptor', ['$q', '$injector', | ||
function ($q, $injector) { | ||
return { | ||
responseError: function(rejection) { | ||
if (!rejection.config.ignoreAuthModule) { | ||
switch (rejection.status) { | ||
case 401: | ||
$injector.get('$state').transitionTo('authentication.signin'); | ||
break; | ||
case 403: | ||
$injector.get('$state').transitionTo('forbidden'); | ||
break; | ||
} | ||
} | ||
// otherwise, default behaviour | ||
return $q.reject(rejection); | ||
} | ||
}; | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Bad Request</h1> | ||
<div class="alert alert-danger" role="alert"> | ||
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> | ||
<span class="sr-only">Error:</span> | ||
You made a bad request | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Forbidden</h1> | ||
<div class="alert alert-danger" role="alert"> | ||
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> | ||
<span class="sr-only">Error:</span> | ||
You are not authorized to access this resource | ||
</div> |
57 changes: 57 additions & 0 deletions
57
modules/core/tests/client/interceptors/auth.interceptor.client.tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
(function() { | ||
describe('authInterceptor', function() { | ||
//Initialize global variables | ||
var authInterceptor, | ||
$q, | ||
$state, | ||
httpProvider; | ||
|
||
// Load the main application module | ||
beforeEach(module(ApplicationConfiguration.applicationModuleName)); | ||
|
||
//Load httpProvider | ||
beforeEach(module(function($httpProvider) { | ||
httpProvider = $httpProvider; | ||
})); | ||
|
||
beforeEach(inject(function(_authInterceptor_, _$q_, _$state_) { | ||
authInterceptor = _authInterceptor_; | ||
$q = _$q_; | ||
$state = _$state_; | ||
spyOn($q,'reject'); | ||
spyOn($state,'transitionTo'); | ||
})); | ||
|
||
it('Auth Interceptor should be object', function() { | ||
expect( typeof authInterceptor).toEqual('object'); | ||
}); | ||
|
||
it('Auth Interceptor should contain responseError function', function() { | ||
expect( typeof authInterceptor.responseError).toEqual('function'); | ||
}); | ||
|
||
it('httpProvider Interceptor should have authInterceptor', function() { | ||
expect(httpProvider.interceptors).toContain('authInterceptor'); | ||
}); | ||
|
||
describe('Forbidden Interceptor', function() { | ||
it('should redirect to forbidden route', function () { | ||
var response = {status:403,config:{}}; | ||
var promise = authInterceptor.responseError(response); | ||
expect($q.reject).toHaveBeenCalled(); | ||
expect($state.transitionTo).toHaveBeenCalledWith('forbidden'); | ||
}); | ||
}); | ||
|
||
describe('Authorization Interceptor', function() { | ||
it('should redirect to signIn page for unauthorized access', function () { | ||
var response = {status:401,config:{}}; | ||
var promise = authInterceptor.responseError(response); | ||
expect($q.reject).toHaveBeenCalled(); | ||
expect($state.transitionTo).toHaveBeenCalledWith('authentication.signin'); | ||
}); | ||
}); | ||
}); | ||
})(); |