如何使用 Amplify 和 Javascript 返回 AWS 用户池中的用户状态?

Ral*_*thy 5 javascript amazon-web-services reactjs aws-amplify

我的用户池中有一些用户: 在此处输入图片说明

如何使用 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 });
  }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Sha*_*aka 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 });
}
Run Code Online (Sandbox Code Playgroud)

  • 最好的选择是在用户登录期间处理“UserNotConfirmedException”异常。此外,您可以根据您的要求检查“verifyCurrentUserAttribute”或“verifyUserAttribute”的状态 - 完整的 API 位于 https://aws.github.io/aws-amplify/api/classes/authclass.html#resendsignup (3认同)