網站搜尋

reactjs - 如何使用 Javascript 與 Amplify 在 AWS 的使用者池中回傳使用者的狀態?


我在我的使用者池中有一些使用者:

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

有什麼想法嗎?

答案#1

我不確定你真正想要什麼,但如果你想找到方法來檢索使用者的狀態資訊以便管理特定的工作流程,你必須檢查 challengeName 和 challengeParam。例如,當你對一個新使用者進行 signIn 時,auth.signIn 會返回一個 cognito 使用者:如果這個使用者尚未確認,該使用者將具有 challengeName 和 challengeParam 屬性,你可以在程式碼中驗證。例如:我從控制台創建了一個使用者並設置了臨時密碼,因此他處於需要新密碼的狀態,我在程式碼中這麼做是為了能夠完成新密碼工作流程。

  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。

希望這能幫助你。

答案#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 });
}