feat(endpoint): auto-provision default API key for first-time users

- Endpoint page auto-creates a "Default Key" when no keys exist yet,
  so /v1 works out of the box without a manual dashboard step
- Show/copy key buttons stay visible instead of opacity-0 by default
This commit is contained in:
decolua
2026-08-05 16:22:06 +07:00
parent dcdd4628b3
commit 02c66fe2bd

View File

@@ -255,11 +255,26 @@ export default function APIPageClient({ machineId }) {
const fetchData = async () => { const fetchData = async () => {
try { try {
const keysRes = await fetch("/api/keys"); const fetchKeys = async () => {
const keysData = await keysRes.json(); const res = await fetch("/api/keys");
if (keysRes.ok) { if (!res.ok) return [];
setKeys(keysData.keys || []); const data = await res.json();
return data.keys || [];
};
let existing = await fetchKeys();
// Auto-provision a default key for first-time users so the endpoint works out of the box.
if (existing.length === 0) {
try {
const createRes = await fetch("/api/keys", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Default Key" }),
});
if (createRes.ok) existing = await fetchKeys();
} catch { /* fall through to empty render */ }
} }
setKeys(existing);
} catch (error) { } catch (error) {
console.log("Error fetching data:", error); console.log("Error fetching data:", error);
} finally { } finally {
@@ -1005,7 +1020,7 @@ export default function APIPageClient({ machineId }) {
</code> </code>
<button <button
onClick={() => toggleKeyVisibility(key.id)} onClick={() => toggleKeyVisibility(key.id)}
className="p-1 hover:bg-black/5 dark:hover:bg-white/5 rounded text-text-muted hover:text-primary opacity-100 sm:opacity-0 sm:group-hover:opacity-100 transition-all" className="p-1 hover:bg-black/5 dark:hover:bg-white/5 rounded text-text-muted hover:text-primary transition-all"
title={visibleKeys.has(key.id) ? "Hide key" : "Show key"} title={visibleKeys.has(key.id) ? "Hide key" : "Show key"}
> >
<span className="material-symbols-outlined text-[14px]"> <span className="material-symbols-outlined text-[14px]">
@@ -1014,7 +1029,7 @@ export default function APIPageClient({ machineId }) {
</button> </button>
<button <button
onClick={() => copy(key.key, key.id)} onClick={() => copy(key.key, key.id)}
className="p-1 hover:bg-black/5 dark:hover:bg-white/5 rounded text-text-muted hover:text-primary opacity-100 sm:opacity-0 sm:group-hover:opacity-100 transition-all" className="p-1 hover:bg-black/5 dark:hover:bg-white/5 rounded text-text-muted hover:text-primary transition-all"
> >
<span className="material-symbols-outlined text-[14px]"> <span className="material-symbols-outlined text-[14px]">
{copied === key.id ? "check" : "content_copy"} {copied === key.id ? "check" : "content_copy"}