'use client'; import { useState, useEffect } from "react"; import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { SendIcon, Loader2, GlobeIcon, ChevronDownIcon, Ship } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem } from "@/components/ui/dropdown-menu" import { useTranslation } from '@/i18n/client' import { useRouter, useParams, usePathname, useSearchParams } from 'next/navigation'; import { InputOTP, InputOTPGroup, InputOTPSlot, } from "@/components/ui/input-otp" import { signIn } from 'next-auth/react'; import { sendOtpAction } from "@/lib/users/send-otp"; import { verifyTokenAction } from "@/lib/users/verifyToken"; import { buttonVariants } from "@/components/ui/button" import Link from "next/link" import Image from 'next/image'; // 추가: Image 컴포넌트 import export function LoginForm({ className, ...props }: React.ComponentProps<"div">) { const params = useParams() || {}; const pathname = usePathname() || ''; const router = useRouter(); const searchParams = useSearchParams(); const token = searchParams?.get('token') || null; const lng = params.lng as string; const { t, i18n } = useTranslation(lng, 'login'); const { toast } = useToast(); const handleChangeLanguage = (lang: string) => { const segments = pathname.split('/'); segments[1] = lang; router.push(segments.join('/')); }; const currentLanguageText = i18n.language === 'ko' ? t('languages.korean') : t('languages.english'); const [email, setEmail] = useState(''); const [otpSent, setOtpSent] = useState(false); const [isLoading, setIsLoading] = useState(false); const [otp, setOtp] = useState(''); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { const result = await sendOtpAction(email, lng); if (result?.success) { setOtpSent(true); toast({ title: t('otpSentTitle'), description: t('otpSentMessage'), }); } } catch (error) { toast({ title: t('errorTitle'), description: t('defaultErrorMessage'), variant: 'destructive', }); } finally { setIsLoading(false); } }; async function handleOtpSubmit(e: React.FormEvent) { e.preventDefault(); setIsLoading(true); try { // next-auth의 Credentials Provider로 로그인 시도 const result = await signIn('credentials', { email, code: otp, redirect: false, // 커스텀 처리 위해 redirect: false }); if (result?.ok) { // 토스트 메시지 표시 toast({ title: t('loginSuccess'), description: t('youAreLoggedIn'), }); // NextAuth에서 유저 정보 API 호출 (최신 상태 보장) const response = await fetch('/api/auth/session'); const session = await response.json(); // domain 값에 따라 동적으로 리다이렉션 const userDomain = session?.user?.domain; console.log(session) if (userDomain === 'evcp') { router.push(`/${lng}/evcp/report`); } else if (userDomain === 'partners') { router.push(`/${lng}/partners/dashboard`); } else { // 기본 리다이렉션 경로 router.push(`/${lng}/dashboard`); } } else { toast({ title: t('errorTitle'), description: t('defaultErrorMessage'), variant: 'destructive', }); } } catch (error) { console.error('Login error:', error); toast({ title: t('errorTitle'), description: t('defaultErrorMessage'), variant: 'destructive', }); } finally { setIsLoading(false); } } useEffect(() => { const verifyToken = async () => { if (!token) return; setIsLoading(true); try { const data = await verifyTokenAction(token); if (data.valid) { setOtpSent(true); setEmail(data.email ?? ''); } else { toast({ title: t('errorTitle'), description: t('invalidToken'), variant: 'destructive', }); } } catch (error) { toast({ title: t('errorTitle'), description: t('defaultErrorMessage'), variant: 'destructive', }); } finally { setIsLoading(false); } }; verifyToken(); }, [token, toast, t]); return (
{/* Left Content */}
{/* Top bar with Logo + eVCP (left) and "Request Vendor Repository" (right) */}
{/* logo */} eVCP
Request Vendor Repository
{/* Content section that occupies remaining space, centered vertically */}
{/* Your form container */}
{/* Here's your existing login/OTP forms: */} {!otpSent ? (

{t('loginMessage')}

setEmail(e.target.value)} />
handleChangeLanguage(value)} > {t('languages.english')} {t('languages.korean')}
) : (

{t('loginMessage')}

setOtp(value)} >
handleChangeLanguage(value)} > {t('languages.english')} {t('languages.korean')}
)}
{t('termsMessage')} {t('termsOfService')} {t('and')} {t('privacyPolicy')}.
{/* Right BG 이미지 영역 - Image 컴포넌트로 수정 */}
{/* Image 컴포넌트로 대체 */}
Background image

“{t("blockquote")}”

{/* */}
) }