【问题标题】:How to return the user's status within AWS's user pool using Amplify with Javascript?如何使用 Amplify with Javascript 返回 AWS 用户池中的用户状态?
【发布时间】:2018-05-17 21:07:18
【问题描述】:

我的用户池中有一些用户:

如何使用 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 });
  }

有什么想法吗?

【问题讨论】:

    标签: javascript reactjs amazon-web-services aws-amplify


    【解决方案1】:

    我不知道您真正想要什么,但如果您想找到方法来检索有关用户状态的信息以管理特定的工作流程,您必须检查 challengeName 和 challengeParam。例如,当您登录一个新用户 auth.signIn 时,返回一个认知用户:如果未确认此用户,则该用户将拥有一个 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 显示的答案 user with "NEW PASSWORD REQUIRED STATUS"

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

    希望这会对你有所帮助。

    【讨论】:

      【解决方案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 });
      }
      

      【讨论】:

      • 欣赏答案。基本上我是在询问是否有任何方法可以根据输入的电子邮件和 AWS 提供的某些方法记录“未确认”。
      • 最好的选择是在用户登录期间处理“UserNotConfirmedException”异常。此外,您可以根据您的要求检查“verifyCurrentUserAttribute”或“verifyUserAttribute”的状态 - 完整的 API 位于 aws.github.io/aws-amplify/api/classes/…
      猜你喜欢
      • 2020-06-13
      • 2021-12-19
      • 2020-05-22
      • 2019-05-06
      • 2020-12-22
      • 2019-10-11
      • 2021-03-09
      • 2020-04-05
      • 2023-02-24
      相关资源
      最近更新 更多