【问题标题】:AWS Cognito authentication bug with JavaScript and React.js使用 JavaScript 和 React.js 的 AWS Cognito 身份验证错误
【发布时间】:2020-09-11 03:39:03
【问题描述】:

我正在尝试修复我的代码中的一个错误,即每当用户使用相同的电子邮件注册我的应用程序两次时,都没有验证可以阻止它:

这是我的 onSubmit 代码

async function handleSubmit(event) {
    event.preventDefault();

    setIsLoading(true);
    try {

      const newUser = await Auth.signUp({
        username: fields.email,
        password: fields.password,
      });

      setIsLoading(false);
      setNewUser(newUser);
    } catch (e) {

      console.log(e)
      if (e.name === 'UsernameExistsException') {
        ///current bug if you signup already existing user, it will succeed
        setNewUser({
          username: fields.email,
          password: fields.password,
        });
        // resend code if user wants ! Add this link
        // Auth.resendSignUp(fields.email)
        setIsLoading(false);
        return
      }
      onError(e);
      setIsLoading(false);
    }
  }

我可以处理此错误,但不幸的是,如果用户第一次注册并在完成电子邮件代码验证过程之前刷新页面,我会在第二次尝试注册时遇到完全相同的错误,因此,阻止他们的注册。

你如何解决这个问题?我读到我们可以尝试登录用户检查他们的属性,然后将他们注销。

这里是有问题的错误:


{code: "UsernameExistsException", name: "UsernameExistsException", message: "An account with the given email already exists."}
code: "UsernameExistsException"
message: "An account with the given email already exists."
name: "UsernameExistsException"

【问题讨论】:

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


    【解决方案1】:

    这是我能够添加的解决方法。任何 cmets/增强功能都将受到高度赞赏。

    async function handleSubmit(event) {
        event.preventDefault();
    
        setIsLoading(true);
    
    
        try {
    
          const newUser = await Auth.signUp({
            username: fields.email,
            password: fields.password,
          });
    
          setIsLoading(false);
          setNewUser(newUser);
        } catch (e) {
    
          console.log(e)
          if (e.name === 'UsernameExistsException') {
    
    
            ///add new try to do a signup simulation
            try {
              await Auth.signIn(fields.email, fields.password);
            } catch (e) {
              if (e.name === 'UserNotConfirmedException') {
                setNewUser({
                  username: fields.email,
                  password: fields.password,
                });
              } else {
                alert(`This Username is already registered`)
                //this username alredy exists so we have to display error 
                // history.push("/");
              }
            }
    
            // resend code if user wants ! Add this link
            // Auth.resendSignUp(fields.email)
            setIsLoading(false);
            return
          }
          onError(e);
          setIsLoading(false);
        }
      }
    

    如果我尝试登录用户,我会收到必要的错误。

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2018-01-29
      • 2016-01-17
      • 2018-05-08
      • 2016-08-20
      • 2017-01-02
      • 2016-06-25
      • 1970-01-01
      • 2019-06-19
      相关资源
      最近更新 更多