Skip to content

Commit

Permalink
Merge pull request #19 from PolymerAWS/feature/loading-loggedin
Browse files Browse the repository at this point in the history
Feature/loading loggedin
  • Loading branch information
bennypowers authored Jul 11, 2017
2 parents 79ed93a + 93b3e51 commit 753517b
Showing 1 changed file with 60 additions and 13 deletions.
73 changes: 60 additions & 13 deletions aws-cognito.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@
notify: true,
},

/**
* True when waiting for a response from Cognito.
* e.g. after submitting login request
*/
loading: {
type: Boolean,
readOnly: true,
notify: true,
},

/**
* Whether or not a user is logged in.
*/
Expand Down Expand Up @@ -151,6 +161,7 @@
*/
signup: function(username, password) {
this._log.info(`Trying to sign up ${username}`);
this._setLoading(true);
const userPool = this._getCognitoUserPool();
let attributeList = [];

Expand All @@ -167,6 +178,7 @@
return new Promise((resolve, reject) => {
userPool
.signUp(username, password, attributeList, null, (error, result) => {
this._setLoading(false);
if (error) { reject(error); }
if (result) { resolve(result); }
});
Expand Down Expand Up @@ -216,6 +228,7 @@
.then(() => {
this._setIdentityId(AWS.config.credentials.params.IdentityId);
this._setUser(this._getCognitoUser(username, userPool));
this._setLoggedIn(true);
})
.then(() => this._getCognitoAttributes(cognitoUser))
.then((attributes) => this._setAttributes(attributes))
Expand Down Expand Up @@ -253,6 +266,7 @@
this._setIdentityId(null);
this._setLoggedIn(false);
this._setUser({username: null});
this._setLoading(false);
},

/**
Expand All @@ -267,27 +281,30 @@
const userPool = this._getCognitoUserPool();
const cognitoUser = userPool.getCurrentUser();
return new Promise((resolve, reject) => {
this._setLoading(true);
if (!cognitoUser) {
let msg = 'No Cognito user found while trying to recover';
this._log.warn(msg);
resolve(msg);
this._setLoading(false);
} else {
const username = cognitoUser.username;
this._log.info( `Cognito user ${cognitoUser.username} found. Trying to recover` );
this._recoverUser(cognitoUser)
.then(() => {
this._setLoading(false);
this._setUser(this._getCognitoUser(username, userPool)); // Refresh the user obj
this._setLoggedIn(true);
this._shouldRefreshUser = !this.noAutoRefresh? true : false;
this._setIdentityId(AWS.config.credentials.params.IdentityId);
this.dispatchEvent(new CustomEvent('restore-session-success', {
detail: username, bubbles: true, composed: true,
}));
this._shouldRefreshUser = !this.noAutoRefresh? true : false;
})
.then(() => {
this._setIdentityId(AWS.config.credentials.params.IdentityId);
this._setUser(this._getCognitoUser(username, userPool)); // Refresh the user obj
})
.then(() => this._getCognitoAttributes(cognitoUser))
.then((attributes) => this._setAttributes(attributes))
.catch((error) => {
this._setLoading(false);
this.dispatchEvent(new CustomEvent('restore-session-failure', {
detail: username, bubbles: true, composed: true,
}));
Expand Down Expand Up @@ -354,9 +371,16 @@
const userPool = this._getCognitoUserPool();
const cognitoUser = this._getCognitoUser(username, userPool);
return new Promise((resolve, reject) => {
this._setLoading(true);
cognitoUser.resendConfirmationCode((error, result) => {
if (error) { reject(error); }
else { resolve(result); }
if (error) {
this._setLoading(false);
reject(error);
}
else {
this._setLoading(false);
resolve(result);
}
});
});
},
Expand All @@ -371,9 +395,16 @@
const userPool = this._getCognitoUserPool();
const cognitoUser = this._getCognitoUser(username, userPool);
return new Promise((resolve, reject) => {
this._setLoading(true);
cognitoUser.forgotPassword({
onSuccess: (result) => resolve(result),
onFailure: (err) => reject(err),
onSuccess: (result) => {
resolve(result);
this._setLoading(false);
},
onFailure: (err) => {
reject(err);
this._setLoading(false);
},
});
});
},
Expand All @@ -390,9 +421,16 @@
const userPool = this._getCognitoUserPool();
const cognitoUser = this._getCognitoUser(username, userPool);
return new Promise((resolve, reject) => {
this._setLoading(true);
cognitoUser.confirmPassword(code, password, {
onSuccess: () => resolve('SUCCESS'),
onFailure: (err) => reject(err),
onSuccess: () => {
resolve('SUCCESS');
this._setLoading(false);
},
onFailure: (err) => {
reject(err);
this._setLoading(false);
},
});
});
},
Expand Down Expand Up @@ -596,7 +634,9 @@
const userPool = this._getCognitoUserPool();
const cognitoUser = this._getCognitoUser(username, userPool);
return new Promise((resolve, reject) => {
this._setLoading(true);
cognitoUser.confirmRegistration(code, true, (error, result) => {
this._setLoading(false);
if (error) { reject(error); }
else { resolve(result); }
});
Expand All @@ -623,9 +663,16 @@
const userPool = this._getCognitoUserPool();
const cognitoUser = this._getCognitoUser(username, userPool);
return new Promise((resolve, reject) => {
this._setLoading(true);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (cognitoSession) => resolve(cognitoSession),
onFailure: (error) => reject(error),
onSuccess: (cognitoSession) => {
this._setLoading(false);
resolve(cognitoSession);
},
onFailure: (error) => {
this._setLoading(false);
reject(error);
},
});
});
},
Expand Down

0 comments on commit 753517b

Please sign in to comment.