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

Commit

Permalink
feat(AuthService): updateToken handles multiple calls
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Apr 10, 2016
1 parent 9c85af7 commit b619953
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 87 deletions.
21 changes: 21 additions & 0 deletions build/tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,24 @@ gulp.task('test', ['lint'], function(done) {
karmaServer.start();
});
});

/**
* Run live test
*/
gulp.task('tdd', function(done) {
server.start(function() {
var karmaServer = new KarmaServer({
configFile: __dirname + '/../../karma.conf.js',
singleRun: false,
browsers: ['Chrome']
}, function(exitCode) {
server.stop(function() {
done();

process.exit(exitCode);
});
});

karmaServer.start();
});
});
85 changes: 43 additions & 42 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AuthService {
}

get auth() {
console.warn('AuthService.auth is deprecated. Use .authentication instead.');
console.warn('DEPRECATED: AuthService.auth. Use .authentication instead.');
return this.authentication;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ export class AuthService {
}

getCurrentToken() {
console.warn('AuthService.getCurrentToken() is deprecated. Use .getAccessToken() instead.');
console.warn('DEPRECATED: AuthService.getCurrentToken(). Use .getAccessToken() instead.');
return this.getAccessToken();
}

Expand Down Expand Up @@ -103,11 +103,7 @@ export class AuthService {
&& this.config.autoUpdateToken
&& this.authentication.accessToken
&& this.authentication.refreshToken) {
if (this.isRefreshing) {
authenticated = true;
} else {
authenticated = this.updateToken();
}
authenticated = this.updateToken();
}

// return as boolean or Promise
Expand Down Expand Up @@ -143,6 +139,38 @@ export class AuthService {
return this.authentication.getPayload();
}

/**
* Request new accesss token
*
* @returns {Promise<Response>} requests new token. can be called multiple times
*
*/
updateToken() {
if (!this.authentication.refreshToken) {
return Promise.reject(new Error('refreshToken not set'));
}

if (this.authentication.updateTokenCallstack.length === 0) {
const content = {
grant_type: 'refresh_token',
refresh_token: this.authentication.refreshToken,
client_id: this.config.clientId ? this.config.clientId : undefined
};

this.client.post(this.config.withBase(this.config.loginUrl), content)
.then(response => {
this.authentication.setTokensFromResponse(response);
this.authentication.resolveUpdateTokenCallstack(response);
})
.catch(err => {
this.authentication.removeTokens();
this.authentication.resolveUpdateTokenCallstack(Promise.reject(err));
});
}

return this.authentication.toUpdateTokenCallstack();
}

/**
* Signup locally
*
Expand All @@ -159,7 +187,7 @@ export class AuthService {
if (typeof arguments[0] === 'object') {
content = arguments[0];
} else {
console.warn('AuthService.signup(displayName, email, password) is deprecated. Provide an object with signup data instead.');
console.warn('DEPRECATED: AuthService.signup(displayName, email, password). Provide an object with signup data instead.');
content = {
'displayName': displayName,
'email': email,
Expand Down Expand Up @@ -195,7 +223,7 @@ export class AuthService {
if (typeof arguments[1] !== 'string') {
content = arguments[0];
} else {
console.warn('AuthService.login(email, password) is deprecated. Provide an object with login data instead.');
console.warn('DEPRECATED: AuthService.login(email, password). Provide an object with login data instead.');
content = {email: email, password: password};
}

Expand Down Expand Up @@ -226,12 +254,13 @@ export class AuthService {
*
*/
logout(redirectUri) {
return this.authentication.logout(redirectUri)
.then(response => {
this.authentication.redirect(redirectUri, this.config.logoutRedirect);
return new Promise(resolve => {
this.authentication.removeTokens();

return response;
});
this.authentication.redirect(redirectUri, this.config.logoutRedirect);

resolve();
});
}

/**
Expand All @@ -240,34 +269,6 @@ export class AuthService {
* @return {Promise<response>}
*
*/
updateToken() {
if (this.isRefreshing) {

}
this.isRefreshing = true;
const refreshToken = this.authentication.refreshToken;
let content = {};

if (refreshToken) {
content = {grant_type: 'refresh_token', refresh_token: refreshToken};
if (this.config.clientId) {
content.client_id = this.config.clientId;
}

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;
});
}

return Promise.reject('refreshToken not enabled');
}

/**
* Authenticate with third-party and redirect to redirectUri (if set) or redirectUri of config
Expand Down
40 changes: 22 additions & 18 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,45 @@ import {OAuth2} from './oAuth2';
@inject(Storage, BaseConfig, OAuth1, OAuth2)
export class Authentication {
constructor(storage, config, oAuth1, oAuth2) {
this.storage = storage;
this.config = config;
this.oAuth1 = oAuth1;
this.oAuth2 = oAuth2;
this.storage = storage;
this.config = config;
this.oAuth1 = oAuth1;
this.oAuth2 = oAuth2;
this.updateTokenCallstack = [];
}

getLoginRoute() {
console.warn('Authentication.getLoginRoute is deprecated. Use baseConfig.loginRoute instead.');
console.warn('DEPRECATED: Authentication.getLoginRoute. Use baseConfig.loginRoute instead.');
return this.config.loginRoute;
}

getLoginRedirect() {
console.warn('Authentication.getLoginRedirect is deprecated. Use baseConfig.loginRedirect instead.');
console.warn('DEPRECATED: Authentication.getLoginRedirect. Use baseConfig.loginRedirect instead.');
return this.config.loginRedirect;
}

getLoginUrl() {
console.warn('Authentication.getLoginUrl is deprecated. Use baseConfig.withBase(baseConfig.loginUrl) instead.');
console.warn('DEPRECATED: Authentication.getLoginUrl. Use baseConfig.withBase(baseConfig.loginUrl) instead.');
return this.config.withBase(this.config.loginUrl);
}

getSignupUrl() {
console.warn('Authentication.getSignupUrl is deprecated. Use baseConfig.withBase(baseConfig.signupUrl) instead.');
console.warn('DEPRECATED: Authentication.getSignupUrl. Use baseConfig.withBase(baseConfig.signupUrl) instead.');
return this.config.withBase(this.config.signupUrl);
}

getProfileUrl() {
console.warn('Authentication.getProfileUrl is deprecated. Use baseConfig.withBase(baseConfig.profileUrl) instead.');
console.warn('DEPRECATED: Authentication.getProfileUrl. Use baseConfig.withBase(baseConfig.profileUrl) instead.');
return this.config.withBase(this.config.profileUrl);
}

getToken() {
console.warn('Authentication.getToken is deprecated. Use .accessToken instead.');
console.warn('DEPRECATED: Authentication.getToken. Use .accessToken instead.');
return this.accessToken;
}

getRefreshToken() {
console.warn('Authentication.getRefreshToken is deprecated. Use .refreshToken instead.');
console.warn('DEPRECATED: Authentication.getRefreshToken. Use .refreshToken instead.');
return this.refreshToken;
}
/* getters/setters for tokens */
Expand Down Expand Up @@ -163,14 +164,17 @@ export class Authentication {
this.refreshToken = null;
}

logout() {
return new Promise(resolve => {
this.removeTokens();

resolve();
});
toUpdateTokenCallstack() {
return new Promise(resolve => this.updateTokenCallstack.push(resolve));
}

resolveUpdateTokenCallstack(response) {
this.updateTokenCallstack.map(resolve => resolve(response));
this.updateTokenCallstack = [];
}


/**
* Authenticate with third-party
*
Expand All @@ -189,12 +193,12 @@ export class Authentication {
redirect(redirectUrl, defaultRedirectUrl) {
// stupid rule to keep it BC
if (redirectUrl === true) {
console.warn('Setting redirectUrl === true to actually not redirect is deprecated. Set redirectUrl===false instead.');
console.warn('DEPRECATED: Setting redirectUrl === true to actually *not redirect* is deprecated. Set redirectUrl === false instead.');
return;
}
// explicit false means don't redirect
if (redirectUrl === false) {
console.warn('Setting redirectUrl === false to actually use the defaultRedirectUrl has changed. It means "Do not redirect" now. Set redirectUrl to undefined or null to use the defaultRedirectUrl.');
console.warn('BREAKING CHANGE: redirectUrl === false means "Do not redirect" now! Set redirectUrl to undefined or null to use the defaultRedirectUrl if so desired.');
return;
}
if (typeof redirectUrl === 'string') {
Expand Down
Loading

0 comments on commit b619953

Please sign in to comment.