【问题标题】:Forgot Password in cognito (if email is not verified)在 cognito 中忘记密码(如果电子邮件未验证)
【发布时间】:2021-02-28 19:49:39
【问题描述】:

我正在尝试使用 Cognito 进行忘记密码流程。但是没有验证电子邮件的用户不会获得忘记密码的验证码。我也检查了其他 StackOverflow 帖子,但建议验证用户的电子邮件,这将是错误的方式,因为它毕竟没有经过验证。

var userData = {
    Username: email,
    Pool: awsConfig.cognitoUserPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);    
    cognitoUser.forgotPassword({
            onSuccess: function (data) {
                // console.log(data);
                resolve(data);
            },
            onFailure: function (error) {
                reject(error);
            }
        });

【问题讨论】:

  • 你想在这里实现什么用例?
  • @AleksanderWons 我的用例是如果用户注册并且没有点击验证链接,如果他点击忘记密码,它不会在电子邮件中发送验证码,因为它只发送到经过验证的邮件。这是一个极端情况,但在我的场景中失败了
  • 因此,您将无法使用 Cognito 做您想做的事。这有点道理。既然用户从未确认帐户的所有权,为什么她/他可以更改密码?
  • 好的@Aleksandra

标签: amazon-web-services amazon-cognito


【解决方案1】:

这是一个迟到的帖子,但我有一个解决方案给你。 重置密码将向已验证的电子邮件发送确认代码。但是如果邮件没有被验证,它会被 async 函数的 catch 块捕获。在此期间,再次将验证链接发送到提供的电子邮件地址(您可以使用 Amplify 的 resendSignup -> https://aws-amplify.github.io/amplify-js/api/classes/authclass.html#resendsignup)。这是因为 Cognito 仅将密码确认代码发送到经过验证的电子邮件地址。 希望这可以帮助您和其他有类似问题的开发人员。下面给出一个代码示例:

const handleSubmit = async () => {
setIsLoading(true)
const isError = handleInputValidation()
if (isError) {
  setIsLoading(false)
  return
}
try {
  await requestCode(input.email)
  setIsLoading(false)
  history.push({
    pathname: path.auth.resetPasswordAwait,
    state: input.email,
  })
} catch (error) {
  if (error.message.includes('verified')) {
    await resendSignUp(input.email)
    setErrorMessage(
      'Your email is not verified. A new verification link is sent to your email. Please confirm your email address.',
    )
  } else {
    setErrorMessage(error.message)
  }
  setIsLoading(false)
}

}

【讨论】:

    猜你喜欢
    • 2015-06-22
    • 2013-03-04
    • 2016-08-31
    • 2013-06-23
    • 2019-12-30
    • 2018-08-17
    • 2011-02-19
    • 2019-10-07
    • 2013-11-07
    相关资源
    最近更新 更多