Files
caddy-proxy-manager/app/(dashboard)/api-tokens/actions.ts
fuomag9 de28478a42 feat: add comprehensive REST API with token auth, OpenAPI docs, and full test coverage
- API token model (SHA-256 hashed, debounced lastUsedAt) with Bearer auth
- Dual auth middleware (session + API token) in src/lib/api-auth.ts
- 23 REST endpoints under /api/v1/ covering all functionality:
  tokens, proxy-hosts, l4-proxy-hosts, certificates, ca-certificates,
  client-certificates, access-lists, settings, instances, users,
  audit-log, caddy/apply
- OpenAPI 3.1 spec at /api/v1/openapi.json with fully typed schemas
- Swagger UI docs page at /api-docs in the dashboard
- API token management integrated into the Profile page
- Fix: next build now works under Node.js (bun:sqlite aliased to better-sqlite3)
- 89 new API route unit tests + 11 integration tests (592 total)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 09:45:45 +01:00

29 lines
946 B
TypeScript

"use server";
import { revalidatePath } from "next/cache";
import { requireUser } from "@/src/lib/auth";
import { createApiToken, deleteApiToken } from "@/src/lib/models/api-tokens";
export async function createApiTokenAction(formData: FormData): Promise<{ rawToken: string } | { error: string }> {
const session = await requireUser();
const userId = Number(session.user.id);
const name = String(formData.get("name") ?? "").trim();
if (!name) {
return { error: "Name is required" };
}
const expiresAt = formData.get("expires_at") ? String(formData.get("expires_at")) : undefined;
const { rawToken } = await createApiToken(name, userId, expiresAt || undefined);
revalidatePath("/profile");
return { rawToken };
}
export async function deleteApiTokenAction(id: number) {
const session = await requireUser();
const userId = Number(session.user.id);
await deleteApiToken(id, userId);
revalidatePath("/profile");
}