Firebase Authentication 만 사용하여 회원가입을 진행하면,
유저의 프로필이나 이름 등 상세 정보를 "저장할 수 없다".
그래서 유저의 상세 정보를 저장하기 위해
Firebase 의 Realtime Database 에 "저장을 할 수 있다".
*하아... 이름불러오려고 쌩쑈를 5~6시간 했음...
https://jjang-j.tistory.com/48
(*블로그 짱잼이의 FE개발 공부 저장소 참고)
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
+갓GPT - Realtime Database사용방법
1. Realtime Database 저장 코드
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
import { getDatabase, ref, set } from "firebase/database";
const auth = getAuth();
const db = getDatabase();
const signUp = async (email, password, displayName) => {
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
// Realtime Database에 데이터 저장
await set(ref(db, `users/${user.uid}`), {
displayName: displayName,
email: user.email,
createdAt: new Date().toISOString()
});
console.log("회원가입 및 데이터 저장 성공");
} catch (error) {
console.error("회원가입 실패:", error.message);
}
};
2. Realtime Database에서 데이터 가져오기
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
import { getDatabase, ref, get } from "firebase/database";
const auth = getAuth();
const db = getDatabase();
const login = async (email, password) => {
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
// Realtime Database에서 유저 데이터 가져오기
const userRef = ref(db, `users/${user.uid}`);
const snapshot = await get(userRef);
if (snapshot.exists()) {
const userData = snapshot.val();
console.log(`환영합니다, ${userData.displayName}님!`);
return userData.displayName;
} else {
console.error("사용자 데이터가 Realtime Database에 없습니다.");
}
} catch (error) {
console.error("로그인 실패:", error.message);
}
};