AWS-Amplify MFA错误“未验证用户”

时间:2018-10-29 15:32:37

标签: javascript reactjs amazon-web-services amazon-cognito aws-amplify

我正在使用ReactJs和带Amplify的AWS-Cognito开发登录系统。我目前有一个登录页面,用户将在其中输入用户名和密码,然后该页面将检查用户是否已启用MFA。如果未启用MFA,它将把MFA设置为TOTP,然后将提示用户在其中输入TOTP。

我当前已将首选MFA设置为SOFTWARE_TOKEN_MFA的代码,但是一旦设置后尝试再次登录,它会引发用户未通过身份验证的错误。通过通过电子邮件发送的验证码对用户进行身份验证,并在注册时完成。

它似乎在“ const userObject =等待Auth.signIn(电子邮件,密码);”处失败。线。它会一直起作用,直到设置了所需的MFA。 TOTP是否也需要确认手机号码还是我的密码有问题?

任何帮助将不胜感激!

handleSubmit = async event => {
event.preventDefault();

try {
  const { email, password } = this.state;

  const userObject = await Auth.signIn(email, password);

  let user = await Auth.currentAuthenticatedUser();
  console.log("user: " + JSON.stringify(user));

  let obj = await Auth.getPreferredMFA(userObject);

  if (obj === "NOMFA") {
    this.state.totp = await Auth.setupTOTP(userObject);
    console.log("totp: " + this.state.totp);

    this.state.authCode = prompt(
      "\nPlease input the TOTP code for 1st time.\n",
      ""
    );

   Auth.verifyTotpToken(userObject, this.state.authCode)
  .then(() => {
    Auth.setPreferredMFA(userObject, "TOTP");
    this.props.history.push("/home");
    this.props.userHasAuthenticated(true);
  })
  .catch(e => {
    // Token is not verified
    this.props.userHasAuthenticated(false);
    alert("TOTP is incorrect \nPlease re-enter Login Details");
  });
  } else {
    this.state.authCode = prompt("\nPlease input the TOTP code.\n", "");

    console.log(this.state.authCode);

    Auth.confirmSignIn(
      userObject,
      this.state.authCode,
      "SOFTWARE_TOKEN_MFA"
    )
      .then(() => {
        this.props.userHasAuthenticated(true);
      })
      .catch(e => {
          alert("TOTP is incorrect \n Please re-enter Login Details");
        }
      });
  }

0 个答案:

没有答案