AWS Cognito completeNewPasswordChallenge调用onFailure方法,但在AWS Console中已确认用户

时间:2018-07-10 15:20:06

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

我在React应用程序中使用AWS Cognito Javascript SDK。我有一个由管理员在AWS控制台中创建的用户,当该用户首次登录时,他们必须重置密码。我经历了newPasswordRequired流程,当我使用参数调用completeNewPasswordChallenge函数时,将运行onFailure回调。当我记录错误时,得到{code: "UnknownError", message: "Unknown error"}。但是,当我检查AWS控制台时,用户池中的用户从FORCE_CHANGE_PASSWORD更改为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>
    );
  }
}

2 个答案:

答案 0 :(得分:0)

我认为您需要在newPasswordRequired回调中调用completeNewPasswordChallenge。

newPasswordRequired: (userAttributes, requiredAttributes) => {
          delete userAttributes.email_verified

          cognitoUser.completeNewPasswordChallenge(newPw, userAttributes, {
            onSuccess: result => {
              AWS.config.credentials.refresh(err => {
                if (err) {
                  throw err
                } else {
                  // do something
                }
              })
            },
            newPasswordRequired: (userAttributes, requiredAttributes) => {
              delete userAttributes.email_verified
              // phone number as well

              cognitoUser.completeNewPasswordChallenge(newPw, userAttributes, this.newPasswordRequired)
            },
            onFailure: err => {
              throw err
            }
          })
        },

答案 1 :(得分:0)

我相信您的帐户上拥有MFA,您需要通过回调处理它: mfaSetup:((challengeName,challengeParameters)=> {...}

当您处理mfaSetup形式的cognitoUser.authenticateUser()回调时,如果需要的话,一切都很好,但是从completeNewPasswordChallenge()回调中,没有类型化的mfaSetup(),我相信aws同事应该尽快修复它。

这就是为什么您的错误代码为空的原因,请在发出请求后检查网络开发工具中的“响应”选项卡。我相信您会找到MFA_SETUP难题来解决。