Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(authService): redirection when token expired optional
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Jun 9, 2016
1 parent 6e13ab3 commit b6ed192
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
2 changes: 2 additions & 0 deletions doc/baseConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class AuthService {
&& this.authentication.getRefreshToken()) {
this.updateToken();
} else {
this.logout();
this.logout(this.config.expiredRedirect);
}
}, ttl);
}
Expand Down
2 changes: 2 additions & 0 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 29 additions & 10 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b6ed192

Please sign in to comment.