Vectrum
Sign In
23 lines · 856 Bytes · TypeScript
import { corsJson, preflight } from "@/lib/cors";
import { OAuthError, registerClient } from "@/lib/oauth";

/** Dynamic client registration (RFC 7591): how an agent introduces itself before asking the user. */
export async function POST(request: Request) {
  let metadata: unknown;
  try {
    metadata = await request.json();
  } catch {
    metadata = null;
  }
  if (typeof metadata !== "object" || metadata === null || Array.isArray(metadata)) {
    return corsJson({ error: "invalid_client_metadata", error_description: "Send the client metadata as a JSON object." }, 400);
  }
  try {
    return corsJson(await registerClient(metadata as Record<string, unknown>), 201);
  } catch (e) {
    if (e instanceof OAuthError) return corsJson({ error: e.code, error_description: e.message }, e.status);
    throw e;
  }
}

export const OPTIONS = preflight;