From 24fdc4c30723e826216982d5cd763ec32f1c0cdf Mon Sep 17 00:00:00 2001 From: sw326 Date: Wed, 11 Sep 2024 16:46:06 +0900 Subject: [PATCH] fix : kakaoLogin fimish --- src/pages/members/Redirection.tsx | 64 +++++++++++++------------------ 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/src/pages/members/Redirection.tsx b/src/pages/members/Redirection.tsx index 3287d88..91e656d 100644 --- a/src/pages/members/Redirection.tsx +++ b/src/pages/members/Redirection.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../../hooks/useAuth'; import { useKakaoLogin } from '../../hooks/useMembers'; @@ -10,31 +10,25 @@ import { const Redirection: React.FC = () => { const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); + const [isLoginAttempted, setIsLoginAttempted] = useState(false); const navigate = useNavigate(); - const { login } = useAuth(); + const { login, isAuthenticated } = useAuth(); const kakaoLoginMutation = useKakaoLogin(); - useEffect(() => { - const kakaoLogin = async () => { - const code = new URL(window.location.href).searchParams.get('code'); - if (!code) { - setError('카카오 인증 코드가 없습니다.'); - setIsLoading(false); - return; - } + const performKakaoLogin = useCallback( + async (code: string) => { + if (isLoginAttempted) return; + setIsLoginAttempted(true); try { const { token, refreshToken } = await kakaoLoginMutation.mutateAsync(code); - // 토큰 저장 및 로그인 처리 localStorage.setItem('token', token); localStorage.setItem('refreshToken', refreshToken); login(token, refreshToken, false); console.log('Login successful:', { token, refreshToken }); - - navigate('/memberhome'); } catch (error) { console.error('Login error:', error); const errorMessage = handleApiError(error); @@ -43,36 +37,32 @@ const Redirection: React.FC = () => { } finally { setIsLoading(false); } - }; + }, + [kakaoLoginMutation, login, isLoginAttempted], + ); - kakaoLogin(); - }, [navigate, login, kakaoLoginMutation]); + useEffect(() => { + const code = new URL(window.location.href).searchParams.get('code'); + if (code && !isAuthenticated && !isLoginAttempted) { + performKakaoLogin(code); + } else if (!code) { + setError('카카오 인증 코드가 없습니다.'); + setIsLoading(false); + } + }, [isAuthenticated, performKakaoLogin, isLoginAttempted]); + + useEffect(() => { + if (isAuthenticated) { + navigate('/memberhome'); + } + }, [isAuthenticated, navigate]); if (isLoading) { - return ( -
-
-

로그인 처리 중입니다. 잠시만 기다려 주세요...

-
-
-
- ); + return
로그인 중...
; } if (error) { - return ( -
-
-

{error}

- -
-
- ); + return
{error}
; } return null;