"use client"; import { useState, type FormEvent } from "react"; import { withBasePath } from "@/utils/base-path"; /** * ฟอร์ม mint ลิงก์ SSO ทดสอบบนหน้า /note/dev (dev-only — route เปิดด้วย env * NOTE_SSO_DEV_MINT=1 เท่านั้น, คำขอผู้ใช้ 2026-10-29 "ใช้ได้ใน note") — server * คำนวณ token/ticket ด้วย secret ของตัวเองแล้วคืน URL เต็ม ค่าลับไม่ผ่านหน้า * เบราว์เซอร์ · ได้ลิงก์มา = คลิกเข้าจริงตาม flow ของทางนั้น (A/B) */ export default function DevLoginLinkForm() { const [mode, setMode] = useState<"jwt" | "ticket">("jwt"); const [userid, setUserid] = useState("oom"); const [username, setUsername] = useState("สรวิชญ์"); const [fid, setFid] = useState("F123"); const [ttl, setTtl] = useState(300); const [fileUrl, setFileUrl] = useState("/samples/01012-672427-1f-690609-0000840223.pdf"); const [typeId, setTypeId] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [url, setUrl] = useState(null); const [copied, setCopied] = useState(false); const onSubmit = async (e: FormEvent) => { e.preventDefault(); setBusy(true); setError(null); setUrl(null); setCopied(false); try { const res = await fetch(withBasePath("/api/note/auth/dev-link"), { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ mode, userid, username, fid, ttl_seconds: ttl, ...(fileUrl.trim() !== "" ? { fileUrl: fileUrl.trim() } : {}), ...(typeId.trim() !== "" ? { typeId: typeId.trim() } : {}), }), }); if (!res.ok) { const data = (await res.json().catch(() => null)) as { error?: string } | null; throw new Error(data?.error ?? `HTTP ${res.status}`); } const data = (await res.json()) as { url: string }; setUrl(data.url); } catch (err) { setError(err instanceof Error ? err.message : "สร้างลิงก์ไม่สำเร็จ"); } finally { setBusy(false); } }; const inputCls = "genesis-input h-9 px-3 py-1.5 disabled:bg-canvas disabled:text-ink-faint"; const labelCls = "text-xs font-medium text-ink-soft"; return (
setTtl(Number(e.target.value))} className={inputCls} />
setUserid(e.target.value)} className={inputCls} required />
setUsername(e.target.value)} className={inputCls} required />
setFid(e.target.value)} className={inputCls} required />
setTypeId(e.target.value)} className={inputCls} placeholder="ไม่กรอก = ไม่มี" />
setFileUrl(e.target.value)} className={inputCls} placeholder="ไม่กรอก = เข้าหน้าแรกอย่างเดียว" />
{error && (

{error}

)} {url && (

ลิงก์พร้อมใช้ (อายุ {ttl} วินาที) — กดเพื่อทดสอบเข้าจริง:

เปิดลิงก์

{url}

)}
); }