5

我的用户池中有一些用户: 在此处输入图像描述

如何使用 Amplify 返回该特定用户的状态?我this.state.email有为用户。

这是我的 handleSubmit 函数的样子:

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

    this.setState({ isLoading: true });

    try {
      const newUser = await Auth.signUp({
        username: this.state.email,
        password: this.state.password,
      });

      this.setState({ newUser });
    } catch (e) {
      if (e.name === 'UserNotConfirmedException') {
        alert(
          'Looks like your account isn\'t confirmed! ' +
          'Please check your email to find the confirmation code.',
        );

        // await Auth.resendSignUp(this.state.email);

        this.setState({
          newUser: {
            username: this.state.email,
            password: this.state.password,
          },
        });
      } else if (e.name === 'UsernameExistsException') {
        // how to check if unconfirmed?
        if ('not sure what to put here') {
          alert(
            'Looks like your account isn\'t confirmed! ' +
            'Please check your email to find the confirmation code.',
          );
          // bring up confirmation page
        } else {
          alert(
            'Looks like you already have an account! ' +
            'Please log in with your current password.',
          );
          this.props.history.push('/login');
        }
      } else {
        alert(e.message);
      }
    }

    this.setState({ isLoading: false });
  }

有任何想法吗?

4

2 回答 2

4

我不知道您真正想要什么,但是如果您想找到方法来检索有关用户状态的信息以管理特定的工作流程,您必须检查 challengeName 和 challengeParam。例如,当您登录一个新用户 auth.signIn 时,返回一个 cognito 用户:如果未确认此用户,则该用户将具有 challengeName 和 challengeParm 属性,您可以在代码中进行验证,例如:我从带有临时密码的控制台,因此他处于需要新密码的状态,从代码中我这样做是为了能够完成新密码工作流程

  return fromPromise(Auth.signIn(username, password)).pipe(
   tap(user => {
     console.log('signIn Methode', user)
      if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
             this.navigate(['/completePassword']);
      }
  }), catchError(..){ }

在下面,您可以看到我从 console.log 用户显示的答案为“需要新密码”

如果用户被确认,您将看不到 challengeName/challangeParm。

希望这会帮助你。

于 2018-05-18T17:52:52.923 回答
2

我没有完全理解你的问题,但我认为我的解释无论如何都会有所帮助。

当您再次尝试注册用户时,您将收到“UsernameExistsException”。

但是,当您尝试使用该用户登录时,会返回“UserNotConfirmedException”。因此,这需要在登录中处理。

你需要的是这样的东西。

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

    this.setState({ isLoading: true });

    try {
        const newUser = await Auth.signUp({
            username: this.state.email,
            password: this.state.password, 'attributes': {
                name: this.state.name,
                email: this.state.email
            }
        });
        this.setState({
            newUser
        });
        } catch (e) {
            if(e.name === 'UsernameExistsException') {
                alert("You are already registered. Resending the verification code to " + this.state.email);
                await Auth.resendSignUp(this.state.email);
                this.state.resent = true;
            } else {
                alert(e.message);
            }
        }

    this.setState({ isLoading: false });
}
于 2018-05-18T03:23:18.657 回答