import firebase from "firebase/compat/app";
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const authService = getAuth();
export default firebase;
영상을 따라하다 안되서 결국엔 검색.
지금은 되지만 나중에 또 안되서 바뀔 수도 있다.
const [isLoggedIn, setIsLoggedIn] = useState(authService.currentUser);
Auth.js 에 로그인 form 만들기
로그인 하기
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});
https://firebase.google.com/docs/auth/web/password-auth?hl=ko
auth 문서에 기본 코드 예시
getAuth 와 createUser~ 매서드를 임포트 받아서 파라미터로 getAuth(), email, password를 넣어주면 로그인이 된다.
프로젝트 Authentication ->User에 들어가면 등록한 아이디를 확인 할 수 있다.
onAuthStateChanged() 매서드 사용
user의 상태를 받아와서 로그인 상태를 구분한다
import { getAuth, onAuthStateChanged } from "firebase/auth";
useEffect(() => {
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
setIsLoggedIn(true);
} else {
setIsLoggedIn(false);
}
setInit(true);
});
});