From 9c85af7971ec9c4154203c3d3fa7613d234936bb Mon Sep 17 00:00:00 2001 From: doktordirk Date: Sun, 10 Apr 2016 15:11:40 +0200 Subject: [PATCH] feat(AuthService): isAuthenticated optionally as promise --- src/authService.js | 42 ++++++++++++++++++++++++++++------------ test/authService.spec.js | 24 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/authService.js b/src/authService.js index 81eebbb..a555fa6 100644 --- a/src/authService.js +++ b/src/authService.js @@ -35,7 +35,7 @@ export class AuthService { /** * Get current user profile from server * - * @param {[{},Number,String]} [criteria object or a Number|String converted to {id:criteria}] + * @param {[{}|number|string]} [criteria object or a Number|String converted to {id:criteria}] * * @return {Promise} * @@ -51,7 +51,7 @@ export class AuthService { * Send current user profile update to server * * @param {any} request body with data. - * @param {[{},Number,String]} [criteria object or a Number|String converted to {id:criteria}] + * @param {[{}|Number|String]} [criteria object or a Number|String converted to {id:criteria}] * * @return {Promise} * @@ -95,15 +95,32 @@ export class AuthService { * @returns {Boolean} true: for Non-JWT tokens and unexpired JWT tokens, false: else * */ - isAuthenticated() { - const isExpired = this.authentication.isTokenExpired(); - if (isExpired && this.config.autoUpdateToken) { + isAuthenticated(asPromise) { + let authenticated = this.authentication.isAuthenticated(); + + // auto-update token? + if (!authenticated + && this.config.autoUpdateToken + && this.authentication.accessToken + && this.authentication.refreshToken) { if (this.isRefreshing) { - return true; + authenticated = true; + } else { + authenticated = this.updateToken(); } - this.updateToken(); } - return this.authentication.isAuthenticated(); + + // return as boolean or Promise + if (asPromise) { + if (authenticated instanceof Promise) return authenticated; + return Promise.resolve(authenticated); + } + + if (authenticated instanceof Promise) { + authenticated.catch(()=>{}).then(Promise.resolve); + return true; + } + return authenticated; } /** @@ -224,6 +241,9 @@ export class AuthService { * */ updateToken() { + if (this.isRefreshing) { + + } this.isRefreshing = true; const refreshToken = this.authentication.refreshToken; let content = {}; @@ -236,15 +256,13 @@ export class AuthService { return this.client.post(this.config.withBase(this.config.loginUrl), content) .then(response => { + this.isRefreshing = false; this.authentication.setTokensFromResponse(response); return response; }).catch(err => { + this.isRefreshing = false; this.authentication.removeTokens(); throw err; - }) - .then(response => { - this.isRefreshing = false; - return response; }); } diff --git a/test/authService.spec.js b/test/authService.spec.js index 7904167..401b404 100644 --- a/test/authService.spec.js +++ b/test/authService.spec.js @@ -30,6 +30,30 @@ function getContainer() { describe('AuthService', () => { + describe('.isAuthenticated()', () => { + afterEach((done) => { + const container = getContainer(); + const authService = container.get(AuthService); + authService.logout().then(done); + }); + + it('should return boolean', () => { + const container = getContainer(); + const authService = container.get(AuthService); + + expect(typeof authService.isAuthenticated()).toBe('boolean'); + }); + + it('should return Promise', (done) => { + const container = getContainer(); + const authService = container.get(AuthService); + + const result = authService.isAuthenticated(true); + expect(result instanceof Promise).toBe(true); + result.then(done); + }); + }); + describe('.signup()', () => { afterEach((done) => { const container = getContainer();