Giter Site home page Giter Site logo

cocotube's Introduction

header

Hi! I'm Chae HaEun🍭

💻 Tech Stack


⚒️ Tools

✏️ Studying



Top Langs

GitHub Streak

footer

cocotube's People

Contributors

chaehaeun avatar

Watchers

 avatar

cocotube's Issues

로그인 확인 모달 버그

💥 문제 상황

import { useEffect, useState } from 'react'
import { authService } from '@/firebase-config'
import { onAuthStateChanged, User } from 'firebase/auth'

const useAuth = () => {
  const [user, setUser] = useState<User | null>(null)

  useEffect(() => {
    const unsubscribe = onAuthStateChanged(authService, authUser => {
      if (authUser) {
        setUser(authUser)
      } else {
        setUser(null)
      }
    })

    return () => unsubscribe()
  }, [])

  return { user }
}

export default useAuth

useAuth 커스텀 훅을 활용해 로그인을 확인하고,

 useEffect(() => {
    if (!user) {
      openModal('로그인이 필요한 페이지입니다.', () => navigate('/signin'))
    }
  }, [user])

SignUpCompletion 컴포넌트에서 로그인이 되어있지 않은 경우 signin페이지로 이동하게 하는 코드를 작성.

하지만 로그인 확인 이전 디폴트 상태인 로그인 x 상태가 먼저 감지되어 로그인이 되어있든 아니든 모달이 뜨며 강제로 로그인 페이지로 이동되는 현상 발생

💡 해결 방법

import { useEffect, useState } from 'react';
import { authService } from '@/firebase-config';
import { onAuthStateChanged, User } from 'firebase/auth';

const useAuth = () => {
  const [user, setUser] = useState<User | null>(null);
  const [isAuthChecked, setIsAuthChecked] = useState<boolean>(false);

  useEffect(() => {
    const unsubscribe = onAuthStateChanged(authService, authUser => {
      setUser(authUser);
      setIsAuthChecked(true);
    });

    return () => unsubscribe();
  }, []);

  return { user, isAuthChecked };
}

export default useAuth;

isAuthChecked라는 상태를 추가하여 로그인 상태가 체크 되었는지 여부를 추적

 useEffect(() => {
    if (isAuthChecked && !user) { // 로그인 상태가 체크되었고 로그인되어 있지 않다면 모달 표시
      openModal('로그인이 필요한 페이지입니다.', () => navigate('/signin'));
    }
  }, [user, isAuthChecked]);

그리고 isAuthChecked 상태가 true일 때만 컴포넌트 내용을 렌더링하도록 함

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.