最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How to return the user's status within AWS's user pool using Amplify with Javascript? - Stack Overflow

programmeradmin0浏览0评论

I have some users in my user pool:

How can I return the status of that particular user with Amplify? I have this.state.email for the user.

Here is what my handleSubmit function looks like:

  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 });
  }

Any ideas?

I have some users in my user pool:

How can I return the status of that particular user with Amplify? I have this.state.email for the user.

Here is what my handleSubmit function looks like:

  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 });
  }

Any ideas?

Share Improve this question asked May 17, 2018 at 21:07 Ralph David AbernathyRalph David Abernathy 5,51813 gold badges56 silver badges87 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I'm not sur what do you really want, but if you want to find way to retrieve the information about the status of the user in order to manage a specific workflow, you have to check the challengeName and challengeParam. For instance, when you signIn a new user, the auth.signIn, return a cognito User: if this user is not confirmed the user will have a challengeName and challengeParm attributes than you can verify within your code for example: I create a user from the console with a temporary password so he was in new password required status, from the code I did this to be able to plete the new password workflow

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

Here below, you can see the answer that I display from the console.log user with "NEW PASSWORD REQUIRED STATUS"

If the user is confirmed you will not see the challengeName/challangeParm.

hope this will help you.

I did not understand your question pletely but I think my explanation will help anyway.

You will get "UsernameExistsException" when you try to Signup the user again.

But, the 'UserNotConfirmedException' is returned when you try to login with this user. Hence this needs to be handled in Sign in.

What you need is something like this.

    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 });
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论