Permalink
Browse files

feat(authService): use authenticated and setTimeout for login status

* authService.authenticated can now be used without dirty checking
* authService.setTimeout can be used to change/set timeout
  • Loading branch information...
doktordirk committed Jun 1, 2016
1 parent 5194096 commit d57e1142069a2170b8c1f3679c094122463281f2
Showing with 90 additions and 0 deletions.
  1. +46 −0 src/authService.js
  2. +44 −0 test/authService.spec.js
@@ -19,6 +19,12 @@ export class AuthService {
*/
config;
/**
* The current login status
* @type {Boolean}
*/
authenticated = false;
constructor(authentication, config) {
this.authentication = authentication;
this.config = config;
@@ -53,10 +59,48 @@ export class AuthService {
}
/**
* sets the login timeout
* @type {Number} timeout time in ms
*/
setTimeout(ttl) {
setTimeout(this.timeout, ttl);
}
/**
* clears the login timeout
*/
clearTimeout() {
clearTimeout(this.timeout);
}
/**
* refresh or unset authenticated after timeout
*/
timeout = () => {
this.clearTimeout();
if (this.config.autoUpdateToken
&& this.authentication.getAccessToken()
&& this.authentication.getRefreshToken()) {
this.updateToken();
} else {
this.authenticated = false;
}
};
/**
* Stores and analyses the servers responseObject. Sets loging status and timeout
* @param {Object} response The servers response as GOJO
*/
setResponseObject(response) {
this.clearTimeout();
this.authentication.setResponseObject(response);
this.authenticated = this.authentication.isAuthenticated();
if (this.authenticated && !Number.isNaN(this.authentication.exp)) {
this.setTimeout(this.getTtl() * 1000);
}
}
/**
@@ -288,6 +332,8 @@ export class AuthService {
logout(redirectUri) {
let localLogout = response => new Promise(resolve => {
this.setResponseObject(null);
clearTimeout(this.timeout);
this.authentication.redirect(redirectUri, this.config.logoutRedirect);
resolve(response);
@@ -6,6 +6,15 @@ import {AuthService} from '../src/aurelia-authentication';
import {BaseConfig} from '../src/baseConfig';
import {Authentication} from '../src/authentication';
const tokenFuture = {
payload: {
name: 'tokenFuture',
admin: true,
exp: '2460017154'
},
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidG9rZW5GdXR1cmUiLCJhZG1pbiI6dHJ1ZSwiZXhwIjoiMjQ2MDAxNzE1NCJ9.iHXLzWGY5U9WwVT4IVRLuKTf65XpgrA1Qq_Jlynv6bc'
};
const noop = () => {};
function getContainer() {
@@ -209,23 +218,58 @@ describe('AuthService', () => {
});
describe('.setTimeout()', () => {
const container = new Container();
let authService = container.get(AuthService);
it('Should queue timeout', done => {
authService.authenticated = true;
authService.setTimeout(0);
expect(authService.authenticated).toBe(true);
setTimeout(done, 0);
});
it('Should have timed out', () => {
expect(authService.authenticated).toBe(false);
});
});
describe('.setResponseObject()', () => {
const container = new Container();
let authService = container.get(AuthService);
authService.getTtl = () => 0;
it('Should set with object', () => {
authService.setResponseObject({access_token: 'some'});
expect(JSON.parse(window.localStorage.getItem('aurelia_authentication')).access_token).toBe('some');
expect(authService.authenticated).toBe(true);
authService.setResponseObject(null);
});
it('Should set with jwt and timeout', 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 delete', () => {
window.localStorage.setItem('aurelia_authentication', 'another');
authService.setResponseObject(null);
expect(window.localStorage.getItem('aurelia_authentication')).toBe(null);
expect(authService.authenticated).toBe(false);
authService.authentication.setResponseObject(null);
});

0 comments on commit d57e114

Please sign in to comment.