본문 바로가기
기초다지기/react

Firebase에서 onAuthStateChanged 메서드를 사용해 사용자 인증상태 변화 감지

by 김빵그 2024. 6. 28.

1. 인증상태변화감지

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/auth.user
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});
firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // 로그인된 상태 - 프로필 페이지 표시
    document.getElementById('loginPage').style.display = 'none';
    document.getElementById('profilePage').style.display = 'block';
    document.getElementById('userEmail').textContent = user.email;
  } else {
    // 로그아웃된 상태 - 로그인 페이지 표시
    document.getElementById('loginPage').style.display = 'block';
    document.getElementById('profilePage').style.display = 'none';
  }
});

 

 

Firebase에서 사용자 관리하기

의견 보내기 Firebase에서 사용자 관리하기 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 사용자 생성 Firebase 프로젝트에서 신규 사용자를 생성할 때는 createU

firebase.google.com

 

'기초다지기 > react' 카테고리의 다른 글

react useNavigate  (1) 2024.07.01
react context  (0) 2024.07.01
firebase 사용자 생성 후 비밀번호 인증  (0) 2024.06.28
firebase onAuthStateChanged  (0) 2024.06.25
firebase Auth  (0) 2024.06.24