Skip to content

Instantly share code, notes, and snippets.

@prasadjay
Created June 5, 2018 17:17
Show Gist options
  • Save prasadjay/40f565de5ce997ee46b6415e743f4a76 to your computer and use it in GitHub Desktop.
Save prasadjay/40f565de5ce997ee46b6415e743f4a76 to your computer and use it in GitHub Desktop.
Change-Password-Cognito
function ChangePassword(username, password, newpassword) {
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
Username: username,
Password: password,
});
var userData = {
Username: username,
Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
cognitoUser.changePassword(password, newpassword, (err, result) => {
if (err) {
console.log(err);
} else {
console.log("Successfully changed password of the user.");
console.log(result);
}
});
},
onFailure: function (err) {
console.log(err);
},
});
}
@renaudfractale
Copy link

merci, cela fonctionne chez moi !

@ejirocodes
Copy link

ejirocodes commented Oct 6, 2021

You may need to get the current user details first if you are getting a callback or user is not authenticated error:

 function getCurrentUser(username) {
  let userPool = new CognitoUserPool({
    Username: username,
    UserPoolId: config.identityPoolId,
    ClientId: config.userPoolWebClientId,
  });

  return userPool.getCurrentUser();
}

more details here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment