import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { getKakaoLoginUrl, kakaoCallback } from '../../utils/api'; interface LoginSectionProps { onBack: () => void; onLogin: () => void; } const LoginSection: React.FC = ({ onBack, onLogin }) => { const { t } = useTranslation(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // 카카오 콜백 처리 (URL에서 code 파라미터 확인) useEffect(() => { const urlParams = new URLSearchParams(window.location.search); const code = urlParams.get('code'); if (code) { handleKakaoCallback(code); } }, []); const handleKakaoCallback = async (code: string) => { setIsLoading(true); setError(null); try { await kakaoCallback(code); // URL에서 code 파라미터 제거 const url = new URL(window.location.href); url.searchParams.delete('code'); window.history.replaceState({}, document.title, url.pathname); // 로그인 성공 onLogin(); } catch (err) { console.error('Kakao callback failed:', err); setError(t('login.kakaoLoginFailed')); // URL에서 code 파라미터 제거 const url = new URL(window.location.href); url.searchParams.delete('code'); window.history.replaceState({}, document.title, url.pathname); } finally { setIsLoading(false); } }; const handleKakaoLogin = async () => { setIsLoading(true); setError(null); try { const response = await getKakaoLoginUrl(); // 카카오 로그인 페이지로 리다이렉트 window.location.href = response.auth_url; } catch (err) { console.error('Failed to get Kakao login URL:', err); setError(t('login.loginUrlFailed')); setIsLoading(false); } }; return (
{/* Back Button */}
{/* Logo */}
ADO2
{/* Error Message */} {error && (
{error}
)} {/* Kakao Login Button */}
); }; export default LoginSection;