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

Commit

Permalink
fix(authService): let authService.isAuthenticated analyse token from …
Browse files Browse the repository at this point in the history
…storage each time (as was intended)

* rename authentication.hasDataStored to more descriptive hasTokenAnalysed
  • Loading branch information
doktordirk committed Aug 17, 2016
1 parent e2251b0 commit e2ef686
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ export class AuthService {
* @returns {Boolean} For Non-JWT and unexpired JWT: true, else: false
*/
isAuthenticated() {
this.authentication.hasTokenAnalyzed = false;

let authenticated = this.authentication.isAuthenticated();

// auto-update token?
Expand Down
27 changes: 13 additions & 14 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Authentication {
this.idToken = null;
this.payload = null;
this.exp = null;
this.hasDataStored = false;
this.hasTokenAnalyzed = false;
}


Expand Down Expand Up @@ -81,13 +81,12 @@ export class Authentication {
this.storage.set(this.config.storageKey, JSON.stringify(response));
return;
}
this.accessToken = null;
this.refreshToken = null;
this.idToken = null;
this.payload = null;
this.exp = null;

this.hasDataStored = false;
this.accessToken = null;
this.refreshToken = null;
this.idToken = null;
this.payload = null;
this.exp = null;
this.hasTokenAnalyzed = false;

this.storage.remove(this.config.storageKey);
}
Expand All @@ -96,27 +95,27 @@ export class Authentication {
/* get data, update if needed first */

getAccessToken() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
if (!this.hasTokenAnalyzed) this.getDataFromResponse(this.getResponseObject());
return this.accessToken;
}

getRefreshToken() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
if (!this.hasTokenAnalyzed) this.getDataFromResponse(this.getResponseObject());
return this.refreshToken;
}

getIdToken() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
if (!this.hasTokenAnalyzed) this.getDataFromResponse(this.getResponseObject());
return this.idToken;
}

getPayload() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
if (!this.hasTokenAnalyzed) this.getDataFromResponse(this.getResponseObject());
return this.payload;
}

getExp() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
if (!this.hasTokenAnalyzed) this.getDataFromResponse(this.getResponseObject());
return this.exp;
}

Expand Down Expand Up @@ -170,7 +169,7 @@ export class Authentication {

this.exp = this.payload ? parseInt(this.payload.exp, 10) : NaN;

this.hasDataStored = true;
this.hasTokenAnalyzed = true;

return {
accessToken: this.accessToken,
Expand Down
26 changes: 26 additions & 0 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,32 @@ describe('AuthService', () => {
expect(typeof result).toBe('boolean');
});

describe('should analyse token from storage each time', () => {
it('should be true after setResponseObject with token', () => {
authService.setResponseObject({token: 'some', refresh_token: 'another'});

expect(authService.isAuthenticated()).toBe(true);
});

it('should be false after clearing storage directly', () => {
authService.authentication.storage.remove(authService.config.storageKey);

expect(authService.isAuthenticated()).toBe(false);
});

it('should be true after setting storage directly', () => {
authService.authentication.storage.set(authService.config.storageKey, JSON.stringify({token: 'some', refresh_token: 'another'}));

expect(authService.isAuthenticated()).toBe(true);
});

it('should be false after setResponseObject with null', () => {
authService.setResponseObject(null);

expect(authService.isAuthenticated()).toBe(false);
});
});

describe('with autoUpdateToken=true', () => {
it('should return boolean true', () => {
authService.setResponseObject({token: 'some', refresh_token: 'another'});
Expand Down
6 changes: 3 additions & 3 deletions test/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('Authentication', () => {
it('Should set data from non-JWT response', () => {
authentication.getDataFromResponse({access_token: 'token'});

expect(authentication.hasDataStored).toBe(true);
expect(authentication.hasTokenAnalyzed).toBe(true);
expect(authentication.accessToken).toBe('token');
expect(authentication.payload).toBe(null);
expect(Number.isNaN(authentication.exp)).toBe(true);
Expand All @@ -358,7 +358,7 @@ describe('Authentication', () => {
it('Should set data from JWT-like response', () => {
authentication.getDataFromResponse({access_token: 'xx.yy.zz'});

expect(authentication.hasDataStored).toBe(true);
expect(authentication.hasTokenAnalyzed).toBe(true);
expect(authentication.accessToken).toBe('xx.yy.zz');
expect(authentication.payload).toBe(null);
expect(Number.isNaN(authentication.exp)).toBe(true);
Expand All @@ -367,7 +367,7 @@ describe('Authentication', () => {
it('Should set data from JWT response', () => {
authentication.getDataFromResponse({access_token: tokenFuture.jwt});

expect(authentication.hasDataStored).toBe(true);
expect(authentication.hasTokenAnalyzed).toBe(true);
expect(authentication.accessToken).toBe(tokenFuture.jwt);
expect(JSON.stringify(authentication.payload)).toBe(JSON.stringify(tokenFuture.payload));
expect(authentication.exp).toBe(Number(tokenFuture.payload.exp));
Expand Down

0 comments on commit e2ef686

Please sign in to comment.