import { exchangeToken, OAuthError } from "@peppa/core"; import { db } from "@/lib/db"; import { json, preflight } from "@/lib/oauth"; export async function POST(req: Request) { const type = req.headers.get("content-type") ?? ""; const params: Record = type.includes("application/json") ? ((await req.json().catch(() => ({}))) as Record) : Object.fromEntries(new URLSearchParams(await req.text())); try { return json(await exchangeToken(db, params)); } catch (e) { if (e instanceof OAuthError) return json({ error: e.error, error_description: e.message }, e.error === "invalid_client" ? 401 : 400); throw e; } } export const OPTIONS = preflight;