Skip to content

Commit

Permalink
Fix regressions which preserved session after logout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benny Powers committed Sep 27, 2017
1 parent 5e6d4d2 commit cf5321a
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions aws-cognito.html
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,18 @@
this._log.info(`Logging out user ${this.user.username}`);
this._shouldRefresh = false;
this.user.signOut();
this._reset();
},

_reset: function() {
this.username = '';
AWS.config.credentials.clearCachedId(); // https://github.com/aws/aws-sdk-js/issues/609
this._setCredentials(null);
this.providers.forEach(() => this.pop('providers'));
this._setLoading(false);
this._setSession(null);
this.providers.forEach(() => this.pop('providers'));
try {
AWS.config.credentials.clearCachedId(); // https://github.com/aws/aws-sdk-js/issues/609
} catch (e) {null;}
},

/**
Expand Down Expand Up @@ -418,10 +425,17 @@
* @param {Boolean} expired Whether or not the credentials are expired.
*/
_computeSession: function(user, expired) {
if (this.session && this.session.isValid()) return;
user && user.username && !expired && this._getSession(user)
.then((session) => this._setSession(session))
.catch((error) => error && this.fireError(error));
if (!this.session && !this.session.isValid() && !expired &&
user && user.username) {
this._getSession(user)
.then((session) => this._setSession(session))
.catch((error) => {
error && this.fireError(error);
this._setSession(null);
});
} else {
this._setSession(null);
}
},

/**
Expand All @@ -430,25 +444,30 @@
* @param {Boolean} loggedIn loggedIn property.
* @return {Boolean}
*/
_computeShouldRefresh: (noAuto, loggedIn) => noAuto == false && loggedIn,
_computeShouldRefresh: function(noAuto, loggedIn) {
return noAuto == false && loggedIn;
},

/**
* Computes a cognitoUser object from the pool.
* @param {String} Username The username.
* @param {CognitoUserPool} Pool The Cognito user pool.
* @return {CognitoUser} The Cognito user.
*/
_computeUser: (Username, Pool) => Username && Pool &&
new CISP.CognitoUser({Username, Pool}),
_computeUser: function(Username, Pool) {
return Username && Pool && new CISP.CognitoUser({Username, Pool}) || null;
},

/**
* Computes a userPool object from User Pool ID and Client ID.
* @param {String} UserPoolId The user pool id.
* @param {String} ClientId The app client id.
* @return {CognitoUserPool} The Cognito User Pool.
*/
_computeUserPool: (UserPoolId, ClientId) => UserPoolId && ClientId &&
new CISP.CognitoUserPool({UserPoolId, ClientId}),
_computeUserPool: function(UserPoolId, ClientId) {
return UserPoolId && ClientId &&
new CISP.CognitoUserPool({UserPoolId, ClientId}) || null;
},

/**
* OBSERVERS
Expand All @@ -466,7 +485,7 @@

_loggedInChanged: function(loggedIn) {
this._setLoading(false);
if (!loggedIn) return;
if (!loggedIn) return this._reset();
this._getAttributes(this.user).then((atts) => this._setAttributes(atts));
this._shouldRefresh = !this.noAutoRefresh ? true : false;
this._refreshIfWillExpire();
Expand All @@ -488,6 +507,7 @@
* @return {undefined}
*/
_sessionChanged: function(session) {
if (!session) return;
const url = `cognito-idp.${this.region}.amazonaws.com/${this.userPoolId}`;
const token = session.getIdToken().getJwtToken();
const provider = {url, token};
Expand Down Expand Up @@ -581,7 +601,7 @@
* the Cognito session.
*/
_getSession: function(user) {
if (!user.username) return Promise.reject();
if (!user.username) return Promise.resolve(null);
return new Promise((resolve, reject) => {
user.getSession((error, sess) => {
if (error) {
Expand Down

0 comments on commit cf5321a

Please sign in to comment.