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

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

我的用户池中有一些用户: enter image description here

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

有什么想法吗?

最佳答案

我不确定您真正想要什么,但是如果您想找到检索有关用户状态的信息以管理特定工作流的方法,则必须检查 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 中显示的答案 user with "NEW PASSWORD REQUIRED STATUS"

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

希望对您有所帮助。

关于javascript - 如何使用 Amplify with Javascript 返回 AWS 用户池中的用户状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50400512/

相关文章:

javascript - 设置传单中图层的缩放级别

javascript - Sails.js 查找记录并将结果存储到 var

python - 如何使用boto获取AWS iam中用户的权限或组详细信息

amazon-web-services - AWS Cloudformation 创建策略卡住

javascript - "pick"函数的 TypeScript 泛型类型(结果对象值类型)

javascript - Javascript 中的字符串模式匹配

javascript - React 无法通过 prop 数组进行映射

reactjs - 在 typescript 中强制执行子组件的属性类型

javascript - Redux reducer 不更新状态对象

amazon-web-services - AWS CentOS SELinux 卡住系统