在导航栏和其他页面按下" sign out "键后试图注销用户,但用户保持不变
这是我一直在使用的AuthContext文件。
import React, {
useContext,
createContext,
useEffect,
useState,
useMemo,
} from "react";
import {
confirmPasswordReset,
createUserWithEmailAndPassword,
FacebookAuthProvider,
GoogleAuthProvider,
onAuthStateChanged,
signInWithPopup,
signOut,
} from "firebase/auth";
import { auth, db } from "../config/firebase";
import {
addDoc,
collection,
getDocs,
onSnapshot,
query,
setDoc,
where,
} from "firebase/firestore";
import { useRouter } from "next/router";
const AuthContext = createContext({
currentUser: null,
signInWithGoogle: () => Promise,
login: () => Promise,
register: () => Promise,
logout: () => Promise,
loading: false,
});
export const useAuth = () => useContext(AuthContext);
export default function AuthContextProvider({ children }) {
const [currentUser, setCurrentUser] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [initalLoading, setInitialLoading] = useState(true);
const [loading, setLoading] = useState(false);
const [uniqueId, setUniqueId] = useState(null);
const route = useRouter();
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
setCurrentUser(user);
setLoading(false);
} else {
setCurrentUser(null);
setLoading(true);
route.push("/");
}
setInitialLoading(false);
});
return () => {
unsubscribe();
};
}, []);
const signInWithGoogle = async () => {
try {
const provider = new GoogleAuthProvider();
provider.setCustomParameters({ prompt: "select_account" });
const result = await signInWithPopup(auth, provider);
// const result = await signInWithRedirect(auth, provider);
const user = result.user;
const q = query(collection(db, "users"), where("uid", "==", user.uid));
const docs = await getDocs(q);
if (docs.docs.length === 0) {
await addDoc(collection(db, "users"), {
uid: user.uid,
name: user.displayName,
authProvider: "facebook",
email: user.email,
});
}
console.log(result.user);
} catch (error) {
console.log(error);
}
};
const signInWithFacebook = async () => {
try {
const provider = new FacebookAuthProvider();
provider.setCustomParameters({ prompt: "select_account" });
const result = await signInWithPopup(auth, provider);
const user = result.user;
const q = query(collection(db, "users"), where("uid", "==", user.uid));
const docs = await getDocs(q);
if (docs.docs.length === 0) {
await addDoc(collection(db, "users"), {
uid: user.uid,
name: user.displayName,
authProvider: "google",
email: user.email,
});
}
console.log(result.user);
} catch (error) {
console.log(error);
}
};
const logout = async () => {
try {
setCurrentUser(null)
await signOut(auth)
return true
}
catch (error) {
return false
}
}
const memoedValue = useMemo(() => ({
currentUser,
signInWithGoogle,
logout,
createUserWithEmailAndPassword,
loading,
signInWithFacebook,
}));
return (
<AuthContext.Provider value={memoedValue}>
{!initalLoading && children}
</AuthContext.Provider>
);
}
* *
这是Firebase配置文件
* *
import { initializeApp, getApp, getApps } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';
import { getStorage } from 'firebase/storage';
// // Your web app's Firebase configuration
const firebaseConfig = {
apiKey: XXX,
authDomain: XXX,
projectId: XXX,
storageBucket: XXX,
messagingSenderId: XXX,
appId: XXX,
measurementId: XXX,
};
const app = !getApps().length ? initializeApp(firebaseConfig) : getApp();
const auth = getAuth(app);
const db = getFirestore(app);
const storage = getStorage(app);
export { app, auth, db, storage };
代码实现是正确的。唯一的问题可能是firebaseConfig
。您没有databaseURL:
属性
databaseURL: process.env.FIREBASE_DATABASE_URL,