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

Commit

Permalink
feat(AuthService): isAuthenticated optionally as promise
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Apr 10, 2016
1 parent aa24516 commit 9c85af7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<response>}
*
Expand All @@ -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<response>}
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -224,6 +241,9 @@ export class AuthService {
*
*/
updateToken() {
if (this.isRefreshing) {

}
this.isRefreshing = true;
const refreshToken = this.authentication.refreshToken;
let content = {};
Expand All @@ -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;
});
}

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

0 comments on commit 9c85af7

Please sign in to comment.