简体   繁体   English

没有从 React 中的自定义 useAuth 挂钩获取更新的错误值

[英]Not getting updated error value from custom useAuth hook in React

I wrote a custom useAuth hook that returns functions like signup, login and an error.我编写了一个自定义 useAuth 挂钩,它返回注册、登录和错误等功能。 But If there is an error such as wrong email and password, it doesn't return an updated error state variable in the Login.js and redirects the user to the /dashboard on form submit even if there is an error.但是,如果出现诸如 email 和密码错误之类的错误,即使出现错误,它也不会在 Login.js 中返回更新后的错误 state 变量并将用户重定向到表单提交时的 /dashboard。

Login.js登录.js

import { useEffect, useState } from "react";
import useAuth from "../hooks/useAuth";
import { useNavigate } from "react-router-dom";

const Login = () => {
  const [user, setUser] = useState({});
  const { login, error } = useAuth();
  const navigate = useNavigate();

  const handleChange = (e) => {
    setUser({ ...user, [e.target.name]: e.target.value });
  };
  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await login(user);
      navigate("/dashboard");
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <div>
      <h1>Log In</h1>
      <form onSubmit={handleSubmit}>
        <div>
          <label htmlFor="email">Email</label>
          <input onChange={handleChange} type="email" name="email" id="email" />
        </div>
        <div>
          <label htmlFor="password">Password</label>
          <input
            onChange={handleChange}
            type="password"
            name="password"
            id="password"
          />
        </div>
        <button>Log In</button>
      </form>
    </div>
  );
};

export default Login;

useAuth.js使用Auth.js

import { useEffect, useState } from "react";
import { account } from "../appwrite/config";
import { ID } from "appwrite";

const useAuth = () => {
  const [error, setError] = useState(null);

  const signup = async (user) => {
    try {
      await account.create(
        ID.unique(),
        user.email,
        user.password,
        user.username
      );
    } catch (error) {
      console.log(error);
      setError(error.message);
    }
  };

  const login = async (user) => {
    setError(null);
    try {
      await account.createEmailSession(user.email, user.password);
    } catch (error) {
      console.log(error);
      setError(error.message);
    }
  };

  const logout = async () => {
    try {
      await account.deleteSession("current");
    } catch (error) {
      console.log(error);
      setError(error.message);
    }
  };
  return { signup, login, logout, error };
};

export default useAuth;

In of the useAuth functions, you need to throw the error inside the catch block so that the error is caught in Login.js .useAuth函数中,您需要将错误抛出到catch块中,以便在Login.js中捕获错误。 For example, try changing your login function to this:例如,尝试将您的login名 function 更改为:

const login = async (user) => {
  setError(null);
  try {
    await account.createEmailSession(user.email, user.password);
  } catch (error) {
    console.log(error);
    setError(error.message);
    throw error; // Error that will be caught in Login.js
  }
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM