11import { useMutation , useQuery } from "@tanstack/react-query" ;
2- import { useEffect , useState } from "react" ;
2+ import { useEffect , useRef , useState } from "react" ;
33
44import { useFlow } from "@/app/stackflow" ;
55
@@ -11,7 +11,8 @@ import { toastError, toastSuccess } from "@/utils/toast";
1111
1212export function useLoginPage ( ) {
1313 const { replace } = useFlow ( ) ;
14- const { setAccessToken, setIsInitialized } = useAuthStore ( ) ;
14+ const { accessToken, isInitialized, setAccessToken, setIsInitialized } =
15+ useAuthStore ( ) ;
1516
1617 const [ isProcessingCode ] = useState ( ( ) => {
1718 const params = new URLSearchParams ( window . location . search ) ;
@@ -25,6 +26,18 @@ export function useLoginPage() {
2526 return false ;
2627 } ) ;
2728
29+ // 로그인 성공 후 store가 실제로 채워지면 navigate
30+ const pendingNavigate = useRef ( false ) ;
31+ useEffect ( ( ) => {
32+ if ( ! pendingNavigate . current || ! accessToken ) return ;
33+ pendingNavigate . current = false ;
34+ if ( isInitialized ) {
35+ replace ( "HomePage" , { } , { animate : false } ) ;
36+ } else {
37+ replace ( "OnboardingNamePage" , { } , { animate : false } ) ;
38+ }
39+ } , [ accessToken , isInitialized , replace ] ) ;
40+
2841 useEffect ( ( ) => {
2942 if ( isKakaoReady ) return ;
3043 const script = document . querySelector < HTMLScriptElement > (
@@ -36,15 +49,11 @@ export function useLoginPage() {
3649 return ( ) => script . removeEventListener ( "load" , onLoad ) ;
3750 } , [ isKakaoReady ] ) ;
3851
39- const handleLoginSuccess = ( accessToken : string , isUser ?: boolean ) => {
52+ const handleLoginSuccess = ( token : string , isUser ?: boolean ) => {
4053 toastSuccess ( "로그인에 성공했어요" ) ;
41- setAccessToken ( accessToken ) ;
54+ setAccessToken ( token ) ;
4255 setIsInitialized ( Boolean ( isUser ) ) ;
43- if ( isUser ) {
44- replace ( "HomePage" , { } , { animate : false } ) ;
45- } else {
46- replace ( "OnboardingNamePage" , { } , { animate : false } ) ;
47- }
56+ pendingNavigate . current = true ;
4857 } ;
4958
5059 const { mutateAsync : loginWithKakao , isPending : isKakaoPending } =
0 commit comments