Skip to content

Commit

Permalink
Merge pull request #18 from PolymerAWS/bugfix/events-composed
Browse files Browse the repository at this point in the history
Bugfix/events composed
  • Loading branch information
bennypowers authored Jun 21, 2017
2 parents 916995d + 7170d2b commit a1cb9b2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 49 deletions.
91 changes: 43 additions & 48 deletions aws-cognito.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@
user: {
type: Object,
readOnly: true,
value: null,
notify: true,
observer: '_dispatchCognitoUser',
},

/**
Expand All @@ -88,9 +86,7 @@
attributes: {
type: Array,
readOnly: true,
value: null,
notify: true,
observer: '_dispatchCognitoAttributes',
},

/**
Expand All @@ -99,7 +95,6 @@
identityId: {
type: String,
readOnly: true,
value: null,
notify: true,
},

Expand Down Expand Up @@ -136,6 +131,11 @@
PolymerElements.LogBehavior,
],

observers: [
'_userChanged(user, identityId)',
'_attributesChanged(attributes)',
],

ready: function() {
this._log.debug(this.localName + ' is ready');
this._initCognito();
Expand Down Expand Up @@ -184,9 +184,13 @@
verify: function(username, code) {
this._confirmRegistration(username, code)
.then((result) =>
this.dispatchEvent(new CustomEvent('confirm-success', {detail: result})))
this.dispatchEvent(new CustomEvent('confirm-success', {
detail: result, bubbles: true, composed: true,
})))
.catch((error) =>
this.dispatchEvent(new CustomEvent('confirm-failure', {detail: error})));
this.dispatchEvent(new CustomEvent('confirm-failure', {
detail: error, bubbles: true, composed: true,
})));
},

/**
Expand Down Expand Up @@ -218,13 +222,15 @@
.then(() => {
// Begin automatically refreshing user unless asked not to
this._shouldRefreshUser = this.noAutoRefresh ? false : true;
this.dispatchEvent(new CustomEvent('login-success', {detail: username}));
this.dispatchEvent(new CustomEvent('login-success', {
detail: username, bubbles: true, composed: true,
}));
resolve({message: 'Successfully logged In', username: username});
})
.catch((error) => {
this._handleError(error);
this.dispatchEvent(new CustomEvent('login-failure', {
detail: error,
detail: error, bubbles: true, composed: true,
}));
});
});
Expand All @@ -246,7 +252,7 @@
this.loggedIn = false;
this._setIdentityId(null);
this._setLoggedIn(false);
this._dispatchCognitoUser({username: null});
this._setUser({username: null});
},

/**
Expand All @@ -269,9 +275,10 @@
const username = cognitoUser.username;
this._log.info( `Cognito user ${cognitoUser.username} found. Trying to recover` );
this._recoverUser(cognitoUser)
// .then(() => this._dispatchCognitoUser(cognitoUser))
.then(() => {
this.dispatchEvent(new CustomEvent('restore-session-success', {detail: username}));
this.dispatchEvent(new CustomEvent('restore-session-success', {
detail: username, bubbles: true, composed: true,
}));
this._shouldRefreshUser = !this.noAutoRefresh? true : false;
})
.then(() => {
Expand All @@ -281,7 +288,9 @@
.then(() => this._getCognitoAttributes(cognitoUser))
.then((attributes) => this._setAttributes(attributes))
.catch((error) => {
this.dispatchEvent(new CustomEvent('restore-session-failure', {detail: username}));
this.dispatchEvent(new CustomEvent('restore-session-failure', {
detail: username, bubbles: true, composed: true,
}));
reject(error);
});
}
Expand Down Expand Up @@ -326,7 +335,7 @@
return new Promise((resolve, reject) => {
this._getCognitoAttributes(cognitoUser)
.then((attributes) => {
this._dispatchCognitoAttributes(attributes);
this._setAttributes(attributes);
resolve(attributes);
})
.catch((error) => reject(error));
Expand Down Expand Up @@ -522,6 +531,8 @@
* @param {Object} error Error object. Should contain `message` key.
*/
_handleError: function(error) {
error.bubbles = true;
error.composed = true;
this.dispatchEvent(new ErrorEvent('error', error));
},

Expand Down Expand Up @@ -673,45 +684,31 @@
* Dispatches an event containing all Cognito user data.
*
* @param {CognitoUser} cognitoUser The Cognito user.
* @param {Object} ov The previous value.
* @param {String} identityId The user's identityId.
* @fires cognito-user
*/
_dispatchCognitoUser: function(cognitoUser, ov) {
if (!cognitoUser && !ov) return; // App is initializing
if (!cognitoUser) {
throw new Error(`No Cognito user found while dispatching user data`);
}
if (!AWS.config.credentials) {
throw new Error( `No Cognito user credentials found while dispatching user` );
_userChanged: function(cognitoUser, identityId) {
if (AWS.config.credentials) {
this._log.debug(`Dispatching Cognito user ${cognitoUser.username}`);
this.dispatchEvent(new CustomEvent('cognito-user', {
detail: {user: cognitoUser, identityId}, bubbles: true, composed: true,
}));
} else {
throw new Error('No Cognito user credentials found while dispatching user');
}
this._log.debug(`Dispatching Cognito user ${cognitoUser.username}`);

const cognitoUserEvent = new CustomEvent('cognito-user', {
detail: {user: cognitoUser, identityId: this.identityId},
bubbles: true,
composed: true,
});
this.dispatchEvent(cognitoUserEvent);
},

/**
* Dispatches an event containing updated user attributes.
*
* @param {CognitoAttributes} attributes The updated attributes.
* @return {CognitoAttributes} The updated attributes.
* @fires cognito-attributes
*/
_dispatchCognitoAttributes: function(attributes) {
if (attributes) {
this._log.debug(`Dispatching Cognito attributes`);
const cognitoAttributesEvent = new CustomEvent('cognito-attributes', {
// detail: formattedAttributes, bubbles: true, composed: true,
detail: attributes, bubbles: true, composed: true,
});
this.dispatchEvent(cognitoAttributesEvent);

return attributes;
}
_attributesChanged: function(attributes) {
this._log.debug(`Dispatching Cognito attributes`);
this.dispatchEvent(new CustomEvent('cognito-attributes', {
detail: attributes, bubbles: true, composed: true,
}));
},

/**
Expand All @@ -726,12 +723,10 @@
throw new Error('No credentials found while dispatching credentials');
}
this._log.debug('Dispatching Cognito credentials');
const cognitoCredentials = new CustomEvent('cognito-credentials', {
detail: {credentials, cognitoUser, cognitoSession},
bubbles: true,
composed: true,
});
this.dispatchEvent(cognitoCredentials);
const detail = {credentials, cognitoUser, cognitoSession};
this.dispatchEvent(new CustomEvent('cognito-credentials', {
detail, bubbles: true, composed: true,
}));
},
});
/**
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "aws-app",
"name": "polymeraws",
"version": "1.0.2",
"authors": [
"Joachim den Hertog",
Expand Down

0 comments on commit a1cb9b2

Please sign in to comment.