简体   繁体   English

即使在输入错误后,React Firebase 也不会返回错误消息

[英]React Firebase is not returning error message even after giving wrong input

I am using React Firebase hook to log in to my website.我正在使用 React Firebase 挂钩登录我的网站。 when trying to log in with the wrong email or password in the login form, an error message will be returned from the React firebase hook.当尝试在登录表单中使用错误的电子邮件或密码登录时,将从 React Firebase 挂钩返回错误消息。 But even after giving the wrong input, an error message is not returning但即使在输入错误后,错误消息也不会返回

const Login = () => {
  const [signInWithEmailAndPassword, error] =
    useSignInWithEmailAndPassword(auth);
    
  const location = useLocation();
  const navigate = useNavigate();

  const from = location?.state?.from?.pathname || '/';
  
  
   if (error) {
     return (
       <div>
         <p>Error: {error.message}</p>
       </div>
     );
   }
    const handleLogIn = (e) => {
      e.preventDefault();
      const email = e.target.email.value;
      const password = e.target.password.value;
      signInWithEmailAndPassword(email, password)
      e.target.reset();
      navigate(from, { replace: true })
      
    }

The signInWithEmailAndPassword appears to be an async function and your code isn't waiting for the returned Promise to resolve. signInWithEmailAndPassword似乎是一个async函数,您的代码没有等待返回的 Promise 解决。 I'm guessing you are seeing the navigate("/");我猜你看到的是navigate("/"); called and the app is navigating to the home page.调用并且应用程序正在导航到主页。

const handleLogIn = (e) => {
  e.preventDefault();
  const email = e.target.email.value;
  const password = e.target.password.value;
  signInWithEmailAndPassword(email, password); // <-- no waiting for promise
  e.target.reset();
  navigate(from, { replace: true }); // <-- navigate away
};

useSignInWithEmailAndPassword 使用SignInWithEmailAndPassword

 export default (auth: Auth): EmailAndPasswordActionHook => { const [error, setError] = useState<AuthError>(); const [loggedInUser, setLoggedInUser] = useState<UserCredential>(); const [loading, setLoading] = useState<boolean>(false); const signInWithEmailAndPassword = async ( email: string, password: string ) => { setLoading(true); setError(undefined); try { const user = await firebaseSignInWithEmailAndPassword( auth, email, password ); setLoggedInUser(user); } catch (err) { setError(err as AuthError); } finally { setLoading(false); } }; const resArray: EmailAndPasswordActionHook = [ signInWithEmailAndPassword, loggedInUser, loading, error, ]; return useMemo<EmailAndPasswordActionHook>(() => resArray, resArray); };

The handleLogin handler should probably wait for the Promise to settle so any errors can be returned by the hook. handleLogin处理程序可能应该等待 Promise 解决,以便挂钩可以返回任何错误。 It turns out though that signInWithEmailAndPassword also doesn't return any resolve/rejected values, so there's no way to know the authentication was successful from within the handleLogIn callback function, the component will need to use the hook's returned loading and loggedInUser states to determine if it is safe to navigate.事实证明,虽然signInWithEmailAndPassword也没有返回任何解析/拒绝值,因此无法从handleLogIn回调函数中知道身份验证是否成功,组件将需要使用钩子返回的loadingloggedInUser状态来确定是否导航是安全的。

Example:例子:

const Login = () => {
  const [
    signInWithEmailAndPassword,
    loggedInUser,
    loading,
    error,
  ] = useSignInWithEmailAndPassword(auth);
    
  const location = useLocation();
  const navigate = useNavigate();

  const from = location?.state?.from?.pathname || '/';

  useEffect(() => {
    if (!loading && loggedInUser) {
      navigate(from, { replace: true });
  }, [loggedInUser, loading, navigate, from]);
    
  if (error) {
    return (
      <div>
        <p>Error: {error.message}</p>
      </div>
    );
  }

  const handleLogIn = (e) => {
    e.preventDefault();
    const email = e.target.email.value;
    const password = e.target.password.value;
    signInWithEmailAndPassword(email, password)
    e.target.reset();
  }

  ...

You are using signInWithEmailAndPassword hook incorrectly.您错误地使用signInWithEmailAndPassword挂钩。 signInWithEmailAndPassword returns an array & 3th index is of error message. signInWithEmailAndPassword返回一个数组,第三个索引是错误消息。

You can follow this: https://github.com/CSFrequency/react-firebase-hooks/blob/master/auth/README.md#usesigninwithemailandpassword你可以按照这个: https ://github.com/CSFrequency/react-firebase-hooks/blob/master/auth/README.md#usesigninwithemailandpassword

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

Since, useSignInWithEmailAndPassword returns an Array, We need to extract/destructure the value from respective index.由于useSignInWithEmailAndPassword返回一个数组,我们需要从各个索引中提取/解构该值。

Apart from that, You must also use loading to display whether firebase is still authorizing the request or not (Loading State).除此之外,您还必须使用loading来显示 firebase 是否仍在授权请求(加载状态)。

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

相关问题 即使刷新页面后反应错误消息仍然存在 - React Error Message Persists even after Refreshing the page 即使在发送消息后,也不会在 React 应用程序中触发 Firebase 云消息传递 onMessage - Firebase cloud messaging onMessage not triggered in React app even after message is sent 反应返回错误解码器消息 - React returning Error decoder message message 使用 React 在输入字段中上传错误的图像类型时显示错误消息 - Display an error message when upload wrong image type in input field using react 由于执行错误,react createref 返回错误 - react createref was returning the error due to wrong implementation 为什么即使在为返回值提供键之后反应键也不起作用 - Why is react key not working even after giving key to the returned value React/firebase:在 jsx 中显示错误消息 - React/firebase: show error message in jsx 在 nginx 中刷新后反应项目给出错误 - React project giving error after refresh in nginx React API 调用给出“你需要启用 JavaScript 来运行这个应用程序。” 在 Firebase 服务器中部署 ReactJs 应用程序后出错 - React API calls giving “You need to enable JavaScript to run this app.” error after deploying ReactJs app in Firebase server 即使在 map 中给出键值后仍显示唯一值错误? - Showing unique value error even after giving the key value in map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM