본문 바로가기
Firebase 호스팅

firebase 게시판 만들기(4)-구글로그인 만들기

by 메씨 2023. 6. 23.
728x90
반응형

1. 로그인 버튼 만들기

<body>
<button class="bt-login btn-primary">Login</button>
<button class="bt-logout btn-danger">LogOut</button>
</body>
/******************************* 글로벌 설정 ********************************/
var auth = firebase.auth();	//firebase의 auth(인증)모듈을 불러온다.
var googleAuth = new firebase.auth.GoogleAuthProvider(); //구글로그인 모듈을 불러온다.

/******************************* 사용자 함수 *******************************/

/******************************* 이벤트 등록 ******************************/
$('.bt-login').click(onLoginGoogle);
$('.bt-logout').click(onLogOut);

/******************************* 이벤트 콜백 *****************************/
function onLogOut(){
    console.log("hi");
    firebase.auth().signOut().then(() => {
        // Sign-out successful.
      }).catch((error) => {
        // An error happened.
      });
      
}

function onLoginGoogle(){
    auth.signInWithPopup(googleAuth);
}

2. 로그인 시 로그아웃 버튼만/ 로그아웃 시 로그인 버튼만 보여지게 하기

/******************************* 글로벌 설정 ********************************/
var auth = firebase.auth();	//firebase의 auth(인증)모듈을 불러온다.
var googleAuth = new firebase.auth.GoogleAuthProvider(); //구글로그인 모듈을 불러온다.
var user = null;

/******************************* 사용자 함수 *******************************/

/******************************* 이벤트 등록 ******************************/
auth.onAuthStateChanged(onChangeAuth);
$('.bt-login').click(onLoginGoogle);
$('.bt-logout').click(onLogOut);

/******************************* 이벤트 콜백 *****************************/
function onChangeAuth(r){
   user = r;
   if(user){
    $('.bt-login').hide();
    $('.bt-logout').show();
   }
   else{
    $('.bt-login').show();
    $('.bt-logout').hide();
   }
}

function onLogOut(){
    console.log("hi");
    firebase.auth().signOut().then(() => {
        // Sign-out successful.
      }).catch((error) => {
        // An error happened.
      });
      
}

function onLoginGoogle(){
    auth.signInWithPopup(googleAuth);
}

로그인 시 이메일 정보와 사용자 사진 불러오기

<div class="info-wrap">
<div class="photo">
<img src="" alt="사진">
</div>
            
<div class="email"></div>
</div>

- img의 사진 크기를 자동으로 조절하여 .photo에 넣어준다.

```css
.info-wrap {
 
}
 .info-wrap .photo {
  width: 36px;
  height: 36px;
  overflow: hidden;
}
 .info-wrap .photo img {
  max-width: 100%;
  height: auto;
  display: block;
}
.info-wrap {
 
}
 .info-wrap .photo {
  width: 36px;
  height: 36px;
  overflow: hidden;
}
 .info-wrap .photo img {
  max-width: 100%;
  height: auto;
  display: block;
}

```

위에서 작성한 함수 onChangeAuth에 넣어준다.

 

function onChangeAuth(r){
   user = r;
   console.log(user);
   if(user){
    $('.info-wrap .email').text(user.email);
    $('.info-wrap .photo img').attr('src', user.photoURL);
    $('.info-wrap').css('display','flex');
    $('.bt-login').hide();
    $('.bt-logout').show();
   }
   else{
    $('.info-wrap .email').text('');
    $('.info-wrap .photo').attr('src', '//via.placeholder.com/1x1/333/');
    $('.info-wrap').css('display','none');
    $('.bt-login').show();
    $('.bt-logout').hide();
    console.log('welcome');
   }
}
728x90
반응형

댓글