Skip to content

Commit e5258c3

Browse files
committed
[refactor] 훅 분리
1 parent 9b8228f commit e5258c3

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

src/domains/login/components/LoginRedirectHandler.tsx

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,11 @@
11
'use client';
22

3-
import { useEffect, useState } from 'react';
4-
import { usePathname, useRouter } from 'next/navigation';
5-
import { getCookie, removeCookie } from '@/domains/shared/auth/utils/cookie';
6-
import { useAuthStore } from '@/domains/shared/store/auth';
73
import Spinner from '@/shared/components/spinner/Spinner';
84
import WelcomeModal from '@/domains/login/components/WelcomeModal';
9-
import { useToast } from '@/shared/components/toast/useToast';
5+
import { useLoginRedirect } from '../hooks/useAuthHooks';
106

117
function LoginRedirectHandler() {
12-
const pathname = usePathname();
13-
const router = useRouter();
14-
const { user, updateUser } = useAuthStore();
15-
const [loading, setLoading] = useState(true);
16-
const [welcomeModalOpen, setWelcomeModalOpen] = useState(false);
17-
const { toastSuccess } = useToast();
18-
19-
useEffect(() => {
20-
if (!user && loading) {
21-
updateUser()
22-
.then((fetchedUser) => {
23-
if (!fetchedUser) {
24-
router.replace('/login');
25-
}
26-
})
27-
.catch(() => {
28-
router.replace('/login');
29-
})
30-
.finally(() => setLoading(false));
31-
} else {
32-
setLoading(false);
33-
}
34-
}, [user, loading, updateUser, router]);
35-
36-
useEffect(() => {
37-
if (!user || loading) return;
38-
39-
const preLoginPath = getCookie('preLoginPath') || '/';
40-
// 로그인 상태인데 이전 페이지가 /login이면 메인으로 이동
41-
if (user && preLoginPath === '/login') {
42-
router.replace('/');
43-
removeCookie('preLoginPath');
44-
return;
45-
}
46-
47-
// 첫 유저일 경우 모달 오픈
48-
if (pathname.startsWith('/login/user/first-user')) {
49-
setWelcomeModalOpen(true);
50-
}
51-
// 기존 유저일 경우
52-
else if (pathname.startsWith('/login/user/success')) {
53-
toastSuccess(`${user.nickname}님 \n 로그인 성공 🎉`);
54-
router.replace(preLoginPath);
55-
removeCookie('preLoginPath');
56-
}
57-
}, [pathname, user, loading, router, toastSuccess]);
58-
59-
// 환영 모달 닫힐 때 이동
60-
const handleCloseWelcomeModal = () => {
61-
setWelcomeModalOpen(false);
62-
const preLoginPath = getCookie('preLoginPath') || '/';
63-
removeCookie('preLoginPath');
64-
router.replace(preLoginPath);
65-
};
8+
const { loading, welcomeModalOpen, handleCloseWelcomeModal, user } = useLoginRedirect();
669

6710
if (loading) {
6811
return (

src/domains/login/hooks/useAuthHooks.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { useAuthStore } from '@/domains/shared/store/auth';
22
import { useToast } from '@/shared/components/toast/useToast';
33
import { useCallback } from 'react';
4+
import { useEffect, useState } from 'react';
5+
import { usePathname, useRouter } from 'next/navigation';
6+
import { getCookie, removeCookie } from '@/domains/shared/auth/utils/cookie';
47

58
export const useLogout = () => {
69
const logout = useAuthStore((state) => state.logout);
@@ -18,3 +21,55 @@ export const useLogout = () => {
1821

1922
return handleLogout;
2023
};
24+
25+
export const useLoginRedirect = () => {
26+
const router = useRouter();
27+
const pathname = usePathname();
28+
const { user, updateUser } = useAuthStore();
29+
const { toastSuccess } = useToast();
30+
31+
const [loading, setLoading] = useState(true);
32+
const [welcomeModalOpen, setWelcomeModalOpen] = useState(false);
33+
34+
useEffect(() => {
35+
if (!user && loading) {
36+
updateUser()
37+
.then((fetchedUser) => {
38+
if (!fetchedUser) router.replace('/login');
39+
})
40+
.catch(() => router.replace('/login'))
41+
.finally(() => setLoading(false));
42+
} else {
43+
setLoading(false);
44+
}
45+
}, [user, loading, updateUser, router]);
46+
47+
useEffect(() => {
48+
if (!user || loading) return;
49+
50+
const preLoginPath = getCookie('preLoginPath') || '/';
51+
52+
if (user && preLoginPath === '/login') {
53+
router.replace('/');
54+
removeCookie('preLoginPath');
55+
return;
56+
}
57+
58+
if (pathname.startsWith('/login/user/first-user')) {
59+
setWelcomeModalOpen(true);
60+
} else if (pathname.startsWith('/login/user/success')) {
61+
toastSuccess(`${user.nickname}님 \n 로그인 성공 🎉`);
62+
router.replace(preLoginPath);
63+
removeCookie('preLoginPath');
64+
}
65+
}, [pathname, user, loading, router, toastSuccess]);
66+
67+
const handleCloseWelcomeModal = () => {
68+
setWelcomeModalOpen(false);
69+
const preLoginPath = getCookie('preLoginPath') || '/';
70+
removeCookie('preLoginPath');
71+
router.replace(preLoginPath);
72+
};
73+
74+
return { loading, welcomeModalOpen, handleCloseWelcomeModal, user };
75+
};

0 commit comments

Comments
 (0)