Permalink
Browse files

fix(authService): let authService.isAuthenticated analyse token from …

…storage each time (as was intended)

* rename authentication.hasDataStored to more descriptive hasTokenAnalysed
  • Loading branch information...
doktordirk committed Aug 3, 2016
1 parent e2251b0 commit e2ef6869bd3da623f3654e88b499874365f82351
Showing with 44 additions and 17 deletions.
  1. +2 −0 src/authService.js
  2. +13 −14 src/authentication.js
  3. +26 −0 test/authService.spec.js
  4. +3 −3 test/authentication.spec.js
@@ -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?
@@ -23,7 +23,7 @@ export class Authentication {
this.idToken = null;
this.payload = null;
this.exp = null;
this.hasDataStored = false;
this.hasTokenAnalyzed = false;
}
@@ -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);
}
@@ -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;
}
@@ -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,
@@ -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'});
@@ -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);
@@ -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);
@@ -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));

0 comments on commit e2ef686

Please sign in to comment.