"use client"; import { useState, type FormEvent } from "react"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { authClient } from "@/lib/auth/client"; export function EmailSettings({ email, verified, callbackCompleted, callbackError }: { email: string; verified: boolean; callbackCompleted: boolean; callbackError: boolean; }) { const [newEmail, setNewEmail] = useState(""); const [busy, setBusy] = useState(false); const [sending, setSending] = useState(false); const [error, setError] = useState(""); const [message, setMessage] = useState(""); async function requestVerification() { if (sending) return; setSending(true); setError(""); setMessage(""); try { const result = await authClient.sendVerificationEmail({ email, callbackURL: "/profile?emailAction=1" }); if (result.error) throw new Error("ส่งลิงก์ยืนยันไม่สำเร็จ กรุณาลองอีกครั้ง"); setMessage("ส่งลิงก์ยืนยันแล้ว กรุณาตรวจสอบอีเมลของคุณ"); } catch (cause) { setError(cause instanceof Error ? cause.message : "ส่งลิงก์ยืนยันไม่สำเร็จ"); } finally { setSending(false); } } async function changeEmail(event: FormEvent) { event.preventDefault(); if (busy) return; setBusy(true); setError(""); setMessage(""); try { const targetEmail = newEmail.trim(); const result = await authClient.changeEmail({ newEmail: targetEmail, callbackURL: "/profile?emailAction=1" }); if (result.error?.code === "SESSION_NOT_FRESH") throw new Error("กรุณาออกจากระบบและเข้าสู่ระบบอีกครั้งก่อนเปลี่ยนอีเมล"); if (result.error) throw new Error("เปลี่ยนอีเมลไม่สำเร็จ กรุณาลองอีกครั้ง"); setNewEmail(""); setMessage(verified ? `ตรวจสอบ ${email} เพื่ออนุมัติการเปลี่ยนแปลง` : `ตรวจสอบ ${targetEmail} เพื่อยืนยันการเปลี่ยนแปลง`); } catch (cause) { setError(cause instanceof Error ? cause.message : "เปลี่ยนอีเมลไม่สำเร็จ"); } finally { setBusy(false); } } return อีเมล

{email} - {verified ? "ยืนยันแล้ว" : "ยังไม่ยืนยัน"}

{callbackCompleted && ยืนยันขั้นตอนแล้ว หากกำลังเปลี่ยนอีเมล โปรดตรวจสอบกล่องจดหมายสำหรับขั้นตอนถัดไป} {callbackError && ลิงก์ยืนยันไม่ถูกต้องหรือหมดอายุ กรุณาขอลิงก์ใหม่} {message && {message}} {error && {error}}
อีเมลใหม่ setNewEmail(event.target.value)} autoComplete="email" required disabled={busy || sending} />
{!verified && }
; }