- Firebase를 사용하여 로그인, 로그아웃 기능을 만들었고, 그에 따른 로그인 상태에 따라 Header를 바꿔보았습니다.
export default function Header() {
const [isOpenModal, setOpenModal] = useState<boolean>(false);
const authService = getAuth();
const uid = authService.currentUser?.uid;
const [authUser, setAuthUser] = useState(null);
const [login, setLogin] = useState(false);
useEffect(() => {
const listen = onAuthStateChanged(auth, (user) => {
if (user) {
setAuthUser(user);
} else {
setAuthUser(null);
}
});
return () => {
listen();
};
}, []);
const logout = () => {
if (login === true) {
return signOut(auth).then(() => {
setLogin(true);
alert("로그아웃 성공 !");
});
} else {
return signOut(auth).catch((error) => {
alert("로그아웃 실패..");
});
}
};
Firestore에 데이터 만들기 및 불러오기
addDoc(collection(dbService, "comments"), {
text: comment.value,
createdAt: Date.now(),
creatorId: uid,
profileImg: photoURL,
nickname: displayName,
});
const q = query(
collection(dbService, "comments"),
orderBy("createdAt", "desc")
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
const commentObj = {
id: doc.id,
...doc.data(),
};
cmtObjList.push(commentObj);
});