'use client'; import { useState, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { createClient } from '@/lib/supabase/client'; import { Suspense } from 'react'; function LoginForm() { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [isSignUp, setIsSignUp] = useState(false); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const router = useRouter(); const searchParams = useSearchParams(); const supabase = createClient(); useEffect(() => { if (searchParams.get('error')) { setMessage('인증 중 오류가 발생했습니다. 다시 시도해주세요.'); } // 이미 로그인된 경우 리다이렉트 supabase.auth.getUser().then(({ data }) => { if (data.user) router.push('/mypage'); }); }, []); const handleAuth = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setMessage(''); if (isSignUp) { const { data, error } = await supabase.auth.signUp({ email, password, options: { emailRedirectTo: `${window.location.origin}/auth/callback`, }, }); if (error) { setMessage('회원가입 실패: ' + error.message); } else if (data.user?.identities?.length === 0) { setMessage('이미 가입된 이메일입니다. 로그인해주세요.'); setIsSignUp(false); } else { setMessage('가입 완료! 이메일 인증 링크를 확인해주세요.'); } } else { const { error } = await supabase.auth.signInWithPassword({ email, password }); if (error) { setMessage('로그인 실패: 이메일 또는 비밀번호를 확인해주세요.'); } else { router.push('/mypage'); router.refresh(); } } setLoading(false); }; const handleGoogleLogin = async () => { // NEXT_PUBLIC_SITE_URL 이 설정되어 있으면 우선 사용 (localhost 리다이렉트 방지) const base = process.env.NEXT_PUBLIC_SITE_URL ?? window.location.origin; const { error } = await supabase.auth.signInWithOAuth({ provider: 'google', options: { redirectTo: `${base}/auth/callback` }, }); if (error) setMessage('Google 로그인 오류: ' + error.message); }; return (
{isSignUp ? '가입 후 사주 기록, 결제 내역을 관리하세요' : '사주 기록·결제·의뢰 내역을 확인하세요'}