"use client" import { useState } from "react" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Field, FieldDescription, FieldGroup, FieldLabel, FieldSeparator, } from "@/components/ui/field" import { Input } from "@/components/ui/input" import { authApi } from "@/lib/api/auth.api" import { toast } from "sonner" import { useRouter } from "next/navigation" export function FindIdForm({ className, ...props }: React.ComponentProps<"form">) { const router = useRouter() const [step, setStep] = useState<"email" | "verify" | "result">("email") const [userName, setUserName] = useState("") const [userEmail, setUserEmail] = useState("") const [code, setCode] = useState("") const [foundUserId, setFoundUserId] = useState("") const [isLoading, setIsLoading] = useState(false) const [timer, setTimer] = useState(0) const handleSendCode = async (e: React.FormEvent) => { e.preventDefault() if (!userName || !userEmail) { toast.error("이름과 이메일을 모두 입력해주세요") return } setIsLoading(true) try { const result = await authApi.sendFindIdCode(userName, userEmail) toast.success(result.message) setStep("verify") setTimer(result.expiresIn) const interval = setInterval(() => { setTimer((prev) => { if (prev <= 1) { clearInterval(interval) return 0 } return prev - 1 }) }, 1000) } catch (error: any) { toast.error(error.message || "인증번호 발송에 실패했습니다") } finally { setIsLoading(false) } } const handleVerifyCode = async (e: React.FormEvent) => { e.preventDefault() if (!code) { toast.error("인증번호를 입력해주세요") return } setIsLoading(true) try { const result = await authApi.verifyFindIdCode(userEmail, code) toast.success(result.message) setFoundUserId(result.maskedUserId || result.userId) setStep("result") } catch (error: any) { toast.error(error.message || "인증번호 확인에 실패했습니다") } finally { setIsLoading(false) } } const formatTime = (seconds: number) => { const m = Math.floor(seconds / 60) const s = seconds % 60 return m + ":" + String(s).padStart(2, "0") } return (
) }