简体   繁体   English

用户电子邮件和密码不匹配时如何显示错误消息

[英]How can I show error message when user email and password doesn't matched

I'm trying to add firebase email-password authentication using firebase and react firebase hooks.我正在尝试使用 firebase 添加 firebase 电子邮件密码身份验证并响应 firebase 挂钩。 I want to show an error message when user try to sign in and the user email and password doesn't matched.当用户尝试登录并且用户电子邮件和密码不匹配时,我想显示一条错误消息。

  const [user] = useAuthState(auth);

  const [signInWithEmailAndPassword, error] = useSignInWithEmailAndPassword(auth);

  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const getEmail = (event) => {
    setEmail(event.target.value);
  };
  const getPassword = (event) => {
    setPassword(event.target.value);
  };
  

  if (error) {
    setEmail("");
    setPassword("");
  }

  return (
      <div className="form-area">        
        <input
          onBlur={getEmail}
          type="email"
          placeholder="Your Email"
        />
        <input
          onBlur={getPassword}
          type="password"
          placeholder="Password"
          required
        />
        <input
          onClick={() => signInWithEmailAndPassword(email, password)}
          type="submit"
          value="Log In"
        />
    </div>
  );
};

Try this out instead of the if statement,试试这个而不是 if 语句,

 const [user] = useAuthState(auth);

  const [signInWithEmailAndPassword, user, loading, error] =
    useSignInWithEmailAndPassword(auth);

  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const getEmail = (event) => {
    setEmail(event.target.value);
  };
  const getPassword = (event) => {
    setPassword(event.target.value);
  };

  return (
      <div className="form-area">        
        <input
          onBlur={getEmail}
          type="email"
          placeholder="Your Email"
        />
        <input
          onBlur={getPassword}
          type="password"
          placeholder="Password"
          required
        />
         {error?.message ? (
            <p
              className="text-danger"
              style={{
                fontSize: "12px",
                fontWeight: "bolder",
                textAlign: "left",
              }}
            >
              Wrong password. Try again!
            </p>
          ) : (
            ""
          )}
        <input
          onClick={() => signInWithEmailAndPassword(email, password)}
          type="submit"
          value="Log In"
        />
    </div>
  );
};
 

Following is the documentation of React Firebase Hooks .以下是React Firebase Hooks的文档。 You can find about displaying error message there.您可以在此处找到有关显示错误消息的信息。

https://github.com/CSFrequency/react-firebase-hooks/tree/master/auth#useauthstate https://github.com/CSFrequency/react-firebase-hooks/tree/master/auth#useauthstate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 密码或用户名错误时如何显示错误消息? - How can I show error message when wrong password or username? 如果密码不匹配,ConfirmPassword 不会显示错误消息 - ConfirmPassword doesnt show the error message in case the password doesn't match 我如何更改 Firebase 中的错误消息? 例如,当输入无效密码时? - How can i change the error message in Firebase? For example, when invalid password is entered? 如何显示从API到最终用户的错误消息? - How can I show error message from API to the end-user? 当用户不刷新页面而只是关闭/退出我的模式时,如何清除登录错误消息? - How can I clear the log in error messages when the user doesn't refresh the page and simply closes/exits out of my modal? 如果用户的 email 和密码不匹配,其他代码将不会运行并返回错误。 但如果这样做,那么它将在谷歌 firebase 中正常工作 - If user's email and password doesn't match the other codes won't run and return an error. But if does, then it will work fine in google firebase 如果密码与数据库中的密码不匹配,如何显示? - How can I display if the password doesn't match the one in the database? 选择照片后如何显示错误消息? - How can I show error message after select photo? 如何删除使用电子邮件和密码在Firebase中注册的用户? 使用React - How can I delete a user registered in Firebase with an email and password? Using React 如何从页面中删除数据并且它不显示? - How can I remove data from the page and it doesn't show?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM