我对 React JS 和 Firebase 非常陌生。我尝试https://medium.com/主题
“在 React 应用程序中构建 Firebase 身份验证和私有路由系统”
修改代码并试错后。我被困在 AuthProvider.js 中。 每次,我尝试从登录或登录页面提交表单..我收到错误消息
createUser 不是一个函数 类型错误:createUser 不是函数 在handleFormSubmit处(http://localhost:3000/main.a5a57c81bfd5076aaa7d.hot-update.js:62:5) 在 HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:29437:18)
这个
createUser
是通过React中的Context传递的。
AuthProvider.js
import {
createUserWithEmailAndPassword,
onAuthStateChanged,
signInWithEmailAndPassword,
signOut,
} from "firebase/auth";
import { createContext, useEffect, useState } from "react";
import PropTypes from "prop-types";
import auth from "./firebaseConfig";
export const AuthContext = createContext("");
const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const createUser = (email, password) => {
setLoading(true);
return createUserWithEmailAndPassword(auth, email, password);
};
const loginUser = (email, password) => {
setLoading(true);
return signInWithEmailAndPassword(auth, email, password);
};
const logOut = () => {
setLoading(true);
return signOut(auth);
};
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
setUser(currentUser);
setLoading(false);
});
return () => {
unsubscribe();
};
}, []);
const authValue = {
createUser,
user,
loginUser,
logOut,
loading,
};
return (
<AuthContext.Provider value={{ authValue }}>
{children}
</AuthContext.Provider>
);
};
AuthProvider.propTypes = {
children: PropTypes.node.isRequired,
};
export default AuthProvider;
注册.js
import { useContext, useState } from "react";
import { AuthContext } from "./AuthProvider";
import { updateProfile } from "firebase/auth";
import { useNavigate } from "react-router-dom";
const SignUp = () => {
const { createUser, user, loading } = useContext(AuthContext);
const [selectedImage, setSelectedImage] = useState(null);
const navigate = useNavigate();
// If authentication is still loading, display a loading indicator
if (loading) {
return (
<span className="loading loading-dots loading-lg flex item-center mx-auto"></span>
);
}
// If the user is already authenticated, redirect to the home page
if (user) {
navigate("/");
}
// Handle form submission for user registration
const handleFormSubmit = (e) => {
e.preventDefault();
const email = e.target.email.value;
const password = e.target.password.value;
createUser(email, password)
.then((result) => {
// Update user profile with display name
updateProfile(result.user, {
//displayName: name,
});
navigate("/");
console.log(result);
})
.catch((error) => {
console.log(error);
});
e.target.reset();
};
// Handle image upload (not shown in the code, but you can add it)
// Render the sign-up form
return (
<div>
<div className="min-h-screen bg-base-200">
<div className="hero-content flex-col">
<div className="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100">
<div className="card-body">
<form onSubmit={handleFormSubmit}>
<div className="form-control">
<label className="label">
<span className="label-text">Name</span>
</label>
<input
type="text"
name="name"
placeholder="Name"
className="input input-bordered"
/>
</div>
<div className="form-control">
<label className="label">
<span className="label-text">Email</span>
</label>
<input
type="email"
name="email"
placeholder="Email"
className="input input-bordered"
/>
</div>
<div className="form-control">
<label className="label">
<span className="label-text">Password</span>
</label>
<input
type="password"
name="password"
placeholder="Password"
className="input input-bordered"
/>
</div>
<div className="form-control mt-6">
<button className="btn btn-primary">Sign Up</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
);
};
export default SignUp;
我尝试通过登录、注册页面使 Firebase Auth 正常工作。此链接指向我的 AuthProvider 页面。 我希望登录用户的信息可以通过Context、React遍历所有组件和页面
由于您要从
createUser
解构 AuthContext
,因此您不应该在 AuthContext.Provider
中使用大括号,因为它将是
value.createUser
。请参阅下面的代码:
<AuthContext.Provider value={authValue}>
docs。