AWS Cognito:我应该如何处理PasswordResetRequiredException

时间:2017-04-29 08:52:29

标签: javascript amazon-web-services amazon-cognito

我在Cognito点击了“重置密码”,现在当我登录时,我得到“PasswordResetRequiredException”,我应该如何处理?我在文档中找不到任何告诉我应该怎么做的内容?

4 个答案:

答案 0 :(得分:4)

答案 1 :(得分:1)

我认为特定用户应该向他们发送电子邮件或短信(如果他们的电子邮件或电话号码已经过验证)。在这封电子邮件中,应该有一个可以与ConfirmForgotPassword一起使用的代码

答案 2 :(得分:0)

您必须为authenticateUser实现newPasswordRequired回调,如下所示:

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        // User authentication was successful
    },

    onFailure: function(err) {
        // User authentication was not successful
    },

    mfaRequired: function(codeDeliveryDetails) {
        // MFA is required to complete user authentication.
        // Get the code from user and call
        cognitoUser.sendMFACode(mfaCode, this)
    },

    newPasswordRequired: function(userAttributes, requiredAttributes) {
        // User was signed up by an admin and must provide new
        // password and required attributes, if any, to complete
        // authentication.

        // the api doesn't accept this field back
        delete userAttributes.email_verified;

        // Get these details and call
        cognitoUser.completeNewPasswordChallenge(newPassword, userAttributes, this);
    }
});

答案 3 :(得分:0)

我想出了可以在onFailure回调中处理此问题的确切方法:

// Create a cognito user instance
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
// Trigger to authenticate the user
cognitoUser.authenticateUser(authenticationDetails, { 
  onFailure: function(err) {
    if (err.code == "PasswordResetRequiredException") {
      // Confirm user data
      cognitoUser.confirmPassword(
        "", // Put your verification code here
        "", // Here is your new password
        {
          onSuccess: result => {
            // Everything worked as expected
          },
          onFailure: err => {
            // Trigger failure
          }
        }
      );
    } else {
      // Trigger failure
    }
  }
});