Permalink
Browse files

feat(authService): redirection when token expired optional

  • Loading branch information...
doktordirk committed Jun 9, 2016
1 parent 6e13ab3 commit b6ed192f36846d1e525a64afc74c1862776ef6db
Showing with 34 additions and 11 deletions.
  1. +2 −0 doc/baseConfig.md
  2. +1 −1 src/authService.js
  3. +2 −0 src/baseConfig.js
  4. +29 −10 test/authService.spec.js
@@ -23,6 +23,8 @@ loginRoute = '/login';
loginOnSignup = true;
// If loginOnSignup == false: The SPA url to which the user is redirected after a successful signup (else loginRedirect is used)
signupRedirect = '#/login';
// redirect when token expires. 0 = don't redirect (default), 1 = use logoutRedirect, string = redirect there
expiredRedirect = 0;
// API related options
@@ -91,7 +91,7 @@ export class AuthService {
&& this.authentication.getRefreshToken()) {
this.updateToken();
} else {
this.logout();
this.logout(this.config.expiredRedirect);
}
}, ttl);
}
@@ -58,6 +58,8 @@ export class BaseConfig {
loginOnSignup = true;
// If loginOnSignup == false: The SPA url to which the user is redirected after a successful signup (else loginRedirect is used)
signupRedirect = '#/login';
// redirect when token expires. 0 = don't redirect (default), 1 = use logoutRedirect, string = redirect there
expiredRedirect = 0;
// API related options
@@ -271,31 +271,50 @@ describe('AuthService', () => {
it('Should set with jwt and not timeout', done => {
spyOn(authService, 'getTtl').and.returnValue(1);
spyOn(authService.authentication, 'redirect').and.callThrough();
authService.setResponseObject({access_token: tokenFuture.jwt});
expect(JSON.parse(window.localStorage.getItem('aurelia_authentication')).access_token).toBe(tokenFuture.jwt);
expect(authService.authenticated).toBe(true);
setTimeout(done, 1);
});
it('Should have timed out', done => {
expect(authService.authenticated).toBe(true);
authService.logout().then(done);
setTimeout(() => {
expect(authService.authenticated).toBe(true);
authService.logout().then(done);
}, 1);
});
it('Should set with jwt and timeout', done => {
spyOn(authService, 'getTtl').and.returnValue(0);
spyOn(authService.authentication, 'redirect').and.callFake((overwriteUri, defaultUri) => {
expect(overwriteUri).toBe(0);
expect(defaultUri).toBe(authService.config.logoutRedirect);
expect(authService.authenticated).toBe(false);
done();
});
authService.setResponseObject({access_token: tokenFuture.jwt});
expect(JSON.parse(window.localStorage.getItem('aurelia_authentication')).access_token).toBe(tokenFuture.jwt);
expect(authService.authenticated).toBe(true);
setTimeout(done, 0);
});
it('Should have timed out', () => {
expect(authService.authenticated).toBe(false);
it('Should set with jwt, timeout and redirect', done => {
spyOn(authService, 'getTtl').and.returnValue(0);
spyOn(authService.authentication, 'redirect').and.callFake((overwriteUri, defaultUri) => {
expect(overwriteUri).toBe(1);
expect(defaultUri).toBe(authService.config.logoutRedirect);
expect(authService.authenticated).toBe(false);
done();
});
authService.setResponseObject({access_token: tokenFuture.jwt});
authService.config.expiredRedirect = 1;
expect(JSON.parse(window.localStorage.getItem('aurelia_authentication')).access_token).toBe(tokenFuture.jwt);
expect(authService.authenticated).toBe(true);
});
it('Should delete', () => {

0 comments on commit b6ed192

Please sign in to comment.