'use client'; import { Suspense, useState } from 'react'; import Link from 'next/link'; import { useRouter, useSearchParams } from 'next/navigation'; import { Loader2 } from 'lucide-react'; import { login } from '@/api/auth'; import { useAuthStore } from '@/store/authStore'; const getSafeRedirectPath = (value: string | null) => { if (!value) return '/'; if (!value.startsWith('/') || value.startsWith('//')) return '/'; if (value.startsWith('/login')) return '/'; if (value.includes('://')) return '/'; return value; }; const getErrorMessage = (error: unknown) => { if (typeof error === 'object' && error !== null && 'response' in error) { const response = (error as { response?: { data?: { message?: string } } }).response; if (response?.data?.message) return response.data.message; } if (error instanceof Error) return error.message; return '로그인 중 오류가 발생했습니다.'; }; function LoginForm() { const router = useRouter(); const searchParams = useSearchParams(); const { login: setLoginState } = useAuthStore(); const redirectPath = getSafeRedirectPath(searchParams.get('redirect')); const [formData, setFormData] = useState({ email: '', password: '' }); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setLoading(true); setError(''); try { const response = await login(formData); if (response.code === 'SUCCESS' && response.data) { setLoginState(response.data.accessToken, response.data.refreshToken); router.push(redirectPath); } else { setError(response.message || '로그인에 실패했습니다.'); } } catch (loginError) { setError(getErrorMessage(loginError)); } finally { setLoading(false); } }; return (
블로그에 오신 것을 환영합니다.