AWS Cognito completeNewPasswordChallenge调用onFailure方法,但在AWS Console中已确认用户 [英] AWS Cognito completeNewPasswordChallenge calls onFailure method but the user is confirmed in AWS Console

查看:155
本文介绍了AWS Cognito completeNewPasswordChallenge调用onFailure方法,但在AWS Console中已确认用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在React应用程序中使用AWS Cognito Javascript SDK。我有一个由管理员在AWS控制台中创建的用户,当该用户首次登录时,他们必须重置密码。我经历了newPasswordRequired流程,当我使用参数调用completeNewPasswordChallenge函数时,将运行onFailure回调。当我记录错误时,得到 {代码: UnknownError,消息:未知错误} 。但是,当我检查AWS控制台时,用户池中的用户从FORCE_CHANGE_PASSWORD更改为CONFIRMED。

I'm using AWS Cognito Javascript SDK in a react application. I have a user that was created in the AWS Console by an admin, and when the user is logged in for the first time they have to reset their password. I go through the newPasswordRequired flow, and when I call the completeNewPasswordChallenge function with the parameters, the onFailure callback is ran. When I log the error I get, {code: "UnknownError", message: "Unknown error"}. However, when I check the AWS Console, the user in the user pool is changed from FORCE_CHANGE_PASSWORD to CONFIRMED.

我的代码是:

class LoginScreenContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isInvalidForm: null,
      isFirstLogin: false,
      user: null,
      userAttr: null
    }
    this.onFormSubmission = this.onFormSubmission.bind(this);
    this.updatePassword = this.updatePassword.bind(this);
  }

  onFormSubmission = (username, password) => {
    const poolData = {
      UserPoolId : AWSConfig.cognito.USER_POOL_ID,
      ClientId : AWSConfig.cognito.APP_CLIENT_ID
    }

    const userPool = new CognitoUserPool(poolData);
    const userData = {
      Username: username,
      Pool: userPool
    }
    const cognitoUser = new CognitoUser(userData);

    const authenticationData = {
        Username : username,
        Password : password
    }
    const authenticationDetails = new AuthenticationDetails(authenticationData);

    cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: (result) => {
        console.log(result);
      },
      onFailure: (err) => {
          console.log("Authenticate user failure");
          console.log(err);
          this.setState({ isInvalidForm: true });
     },
      newPasswordRequired: (userAttributes) => {
         delete userAttributes.email_verified;
         delete userAttributes.phone_number_verified;

        userAttributes.name = authenticationDetails.username;
        console.log(userAttributes);
        this.setState({
          isFirstLogin: true,
          user: cognitoUser,
          userAttr: userAttributes
        });
      }
    });
  }

  updatePassword = (newPassword) => {
    const cognitoUser = this.state.user;
    const userAttr = this.state.userAttr;
    cognitoUser.completeNewPasswordChallenge(newPassword, userAttr, {
      onSuccess: (result) => {
        console.log("NEW PASSWORD COMPLETED: ");
        console.log(result);
      },
      onFailure: (err) => {
        console.log(err);
      }
    });
  }

  render() {
    return (
      <div>
      {this.state.isFirstLogin ? (
        <NewPasswordForm updatePassword={this.updatePassword} />
      ) : (
        <LoginScreenComponent isInvalidForm={this.state.isInvalidForm} onFormSubmission={this.onFormSubmission}/>
      )}
      </div>
    );
  }
}


推荐答案

I相信您的帐户上拥有MFA,您需要通过回调进行处理:

I believe you have MFA on your account and you need to handle it from callback:

mfaSetup: (challengeName, challengeParameters) => { ... }

处理 mfaSetup 形式 cognitoUser.authenticateUser()回调如果需要的话,一切都很好,但是从 completeNewPasswordChallenge()回调那里在输入中没有 mfaSetup(),我相信AWS同事应该尽快修复它。

When you're handling mfaSetup form cognitoUser.authenticateUser() callback all is good if it's required, but from completeNewPasswordChallenge() callback there is no mfaSetup() in typings, which I believe AWS colleagues should fix it ASAP.

这就是为什么空的错误代码,请在您提出的要求后检查网络开发工具中的响应选项卡。我相信您会发现有 MFA_SETUP 难题可以解决。

That's why you have empty error code, please check response tab in network dev tools on post req you made. I believe you'll find there MFA_SETUP challenge to solve.

这篇关于AWS Cognito completeNewPasswordChallenge调用onFailure方法,但在AWS Console中已确认用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆