33import { useEffect , useState } from 'react' ;
44import { usePathname , useRouter } from 'next/navigation' ;
55import { useAuthStore } from '@/shared/@store/auth' ;
6- import { useModalStore } from '@/shared/@store/modal' ;
76import { customToast } from '@/shared/components/toast/CustomToastUtils' ;
87import Spinner from '../spinner/Spinner' ;
8+ import WelcomeModal from './WelcomeModal' ;
9+ import { getCookie , removeCookie } from './utils/cookie' ;
910
1011function LoginRedirectHandler ( ) {
1112 const pathname = usePathname ( ) ;
1213 const router = useRouter ( ) ;
1314 const { user, updateUser } = useAuthStore ( ) ;
14- const { openWelcomeModal } = useModalStore ( ) ;
1515 const [ loading , setLoading ] = useState ( true ) ;
16+ const [ welcomeModalOpen , setWelcomeModalOpen ] = useState ( false ) ;
1617
1718 useEffect ( ( ) => {
1819 if ( ! user && loading ) {
@@ -32,15 +33,28 @@ function LoginRedirectHandler() {
3233 useEffect ( ( ) => {
3334 if ( ! user || loading ) return ;
3435
35- const preLoginPath = sessionStorage . getItem ( 'preLoginPath' ) || '/' ;
36+ const preLoginPath = getCookie ( 'preLoginPath' ) || '/' ;
37+ console . log ( preLoginPath ) ;
3638
39+ // 첫 유저일 경우 모달 오픈
3740 if ( pathname . startsWith ( '/login/first-user' ) ) {
38- openWelcomeModal ( user . nickname ) ;
39- } else if ( pathname . startsWith ( '/login/success' ) ) {
41+ setWelcomeModalOpen ( true ) ;
42+ }
43+ // 기존 유저일 경우
44+ else if ( pathname . startsWith ( '/login/success' ) ) {
4045 customToast . success ( `${ user . nickname } 님 \n 로그인 성공 🎉` ) ;
4146 router . replace ( preLoginPath ) ;
47+ removeCookie ( 'preLoginPath' ) ;
4248 }
43- } , [ pathname , user , router , openWelcomeModal , loading ] ) ;
49+ } , [ pathname , user , loading , router ] ) ;
50+
51+ // 환영 모달 닫힐 때 이동
52+ const handleCloseWelcomeModal = ( ) => {
53+ setWelcomeModalOpen ( false ) ;
54+ const preLoginPath = getCookie ( 'preLoginPath' ) || '/' ;
55+ removeCookie ( 'preLoginPath' ) ;
56+ router . replace ( preLoginPath ) ;
57+ } ;
4458
4559 if ( loading ) {
4660 return (
@@ -50,6 +64,17 @@ function LoginRedirectHandler() {
5064 ) ;
5165 }
5266
53- return null ;
67+ return (
68+ < >
69+ { /* 첫 유저 모달 */ }
70+ { user && (
71+ < WelcomeModal
72+ userNickname = { user . nickname }
73+ open = { welcomeModalOpen }
74+ onClose = { handleCloseWelcomeModal }
75+ />
76+ ) }
77+ </ >
78+ ) ;
5479}
5580export default LoginRedirectHandler ;
0 commit comments