【问题标题】:AWS Cognito: How should I handle PasswordResetRequiredExceptionAWS Cognito:我应该如何处理 PasswordResetRequiredException
【发布时间】:2017-09-27 08:47:39
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    【讨论】:

    • 这里的问题是,当您在 Cognito 上单击“重置密码”时,它会向用户发送一封带有验证码的电子邮件。当您调用 forgotPassword() 时,它会通过电子邮件发送另一个验证码。因此,用户收到 2 封带有 2 个不同代码的电子邮件。在我看来,虽然没什么大不了的,但不是最好的用户体验。有没有办法避免收到 2 封电子邮件?
    【解决方案2】:

    我想出了在 (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
        }
      }
    });
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        您必须为 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);
            }
        });
        

        【讨论】:

        • 我做了,但它没有进入回调。我认为这仅用于新帐户?
        • 没错,回调“newPasswordRequired”仅适用于用户第一次登录时,因为它是最近通过 API 创建的。问题是要问的是不同的流程
        • @LeonardoG。 - PasswordResetRequiredException 不仅适用于新帐户。管理员可以设置“重置密码”标志,该标志发送 PasswordResetRequiredException 而无需回调。
        猜你喜欢
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 2018-12-20
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        相关资源
        最近更新 更多