Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rxTokenInjector): Simple $http injectors for auth and api calls.
- Loading branch information
1 parent
3ac7007
commit 46b9d3c
Showing
10 changed files
with
154 additions
and
0 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
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,3 @@ | ||
[![unstable](http://badges.github.io/stability-badges/dist/unstable.svg)](http://github.com/badges/stability-badges) | ||
|
||
Simple $http interceptor which adds auth token id to http requests. |
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,8 @@ | ||
<pre> | ||
<code> | ||
angular.module('encoreApp', ['encore.ui']) | ||
.config(function ($httpProvider) { | ||
$httpProvider.interceptors.push('TokenInterceptor'); | ||
}); | ||
</code> | ||
</pre> |
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,26 @@ | ||
angular.module('encore.ui.rxTokenInterceptor', ['encore.ui.rxSession']) | ||
/** | ||
* | ||
* @ngdoc service | ||
* @name encore.ui.rxTokenInjector:TokenInjector | ||
* @description | ||
* Simple injector which will intercept http request and inject the | ||
* Rackspace Identity's token into every http request. | ||
* | ||
* @requires encore.ui.rxSession:Session | ||
* | ||
* @example | ||
* <pre> | ||
* angular.module('encoreApp', ['encore.ui']) | ||
* .config(function ($httpProvider) { | ||
* $httpProvider.interceptors.push('TokenInterceptor'); | ||
* }); | ||
* </pre> | ||
*/ | ||
.factory('TokenInterceptor', function (Session) { | ||
return { | ||
request: function (config) { | ||
config.headers['X-Auth-Token'] = Session.getTokenId(); | ||
} | ||
}; | ||
}); |
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,28 @@ | ||
/* jshint node: true */ | ||
|
||
describe('rxTokenInterceptor', function () { | ||
var interceptor, session; | ||
|
||
beforeEach(function () { | ||
// load module | ||
module('encore.ui.rxTokenInterceptor'); | ||
module('encore.ui.rxSession'); | ||
|
||
inject(function ($injector) { | ||
interceptor = $injector.get('TokenInterceptor'); | ||
session = $injector.get('Session'); | ||
session.getTokenId = sinon.stub().returns('12345'); | ||
}); | ||
}); | ||
|
||
it('Interceptor should exist', function () { | ||
expect(interceptor).to.exist; | ||
}); | ||
|
||
it('Interceptor sets X-Auth-Token in headers', function () { | ||
var config = { headers: {}}; | ||
interceptor.request(config); | ||
expect(config.headers).not.be.empty; | ||
expect(config.headers['X-Auth-Token']).to.eq('12345'); | ||
}); | ||
}); |
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,3 @@ | ||
[![unstable](http://badges.github.io/stability-badges/dist/unstable.svg)](http://github.com/badges/stability-badges) | ||
|
||
Simple $http interceptor which will redirect users back to login on 401. |
8 changes: 8 additions & 0 deletions
8
src/rxUnauthorizedInterceptor/docs/rxUnauthorizedInterceptor.html
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,8 @@ | ||
<pre> | ||
<code> | ||
angular.module('encoreApp', ['encore.ui']) | ||
.config(function ($httpProvider) { | ||
$httpProvider.interceptors.push('UnauthorizedInterceptor'); | ||
}); | ||
</code> | ||
</pre> |
31 changes: 31 additions & 0 deletions
31
src/rxUnauthorizedInterceptor/rxUnauthorizedInterceptor.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,31 @@ | ||
angular.module('encore.ui.rxUnauthorizedInterceptor', []) | ||
/** | ||
* | ||
* @ngdoc service | ||
* @name encore.ui.rxUnauthorizedInterceptor:UnauthorizedInterceptor | ||
* @description | ||
* Simple injector which will intercept http responses. If a 401 is returned, | ||
* the ui redirects to /login. | ||
* | ||
* @requires $q | ||
* @requires @window | ||
* | ||
* @example | ||
* <pre> | ||
* angular.module('encoreApp', ['encore.ui']) | ||
* .config(function ($httpProvider) { | ||
* $httpProvider.interceptors.push('UnauthorizedInterceptor'); | ||
* }); | ||
* </pre> | ||
*/ | ||
.factory('UnauthorizedInterceptor', function ($q, $window) { | ||
return { | ||
responseError: function (response) { | ||
if (response.status === 401) { | ||
$window.location = '/login'; | ||
} | ||
|
||
return $q.reject(response); | ||
} | ||
}; | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/rxUnauthorizedInterceptor/rxUnauthorizedInterceptor.spec.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,35 @@ | ||
/* jshint node: true */ | ||
|
||
describe('rxUnauthorizedInterceptor', function () { | ||
var interceptor, mockWindow = {}, | ||
q = { reject: sinon.spy() }; | ||
|
||
beforeEach(function () { | ||
module('encore.ui.rxUnauthorizedInterceptor', | ||
function ($provide) { | ||
$provide.value('$window', mockWindow); | ||
$provide.value('$q', q); | ||
}); | ||
|
||
inject(function ($injector) { | ||
interceptor = $injector.get('UnauthorizedInterceptor'); | ||
}); | ||
}); | ||
|
||
it('Unauthorized Interceptor shoud exist', function () { | ||
expect(interceptor).to.exist; | ||
}); | ||
|
||
it('Interceptor handles error responses', function () { | ||
var response = { status: 500 }; | ||
interceptor.responseError(response); | ||
|
||
expect(mockWindow.location).to.not.eq('/login'); | ||
expect(q.reject.called).to.be.true; | ||
|
||
response.status = 401; | ||
interceptor.responseError(response); | ||
expect(mockWindow.location).to.eq('/login'); | ||
expect(q.reject.called).to.be.true; | ||
}); | ||
}); |