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

Commit

Permalink
feat(authService): use authenticated and setTimeout for login status
Browse files Browse the repository at this point in the history
* 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 d57e114
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit d57e114

Please sign in to comment.