import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import { t as OptionsRepository } from "../../../../../options-DTTML-Tx.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../../parse-GmjecGct.mjs";
import "../../../../../redirects-DXEXBAII.mjs";
import { D as passkeyVerifyBody } from "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { n as getPublicOrigin } from "../../../../../public-url-Danv47cS.mjs";
import { n as validateAllowedOrigins, t as getConfiguredAllowedOrigins } from "../../../../../allowed-origins-CRsJ6BPM.mjs";
import { n as createChallengeStore } from "../../../../../challenge-store-anhwVER0.mjs";
import { t as getPasskeyConfig } from "../../../../../passkey-config-De0ahnRg.mjs";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import { PasskeyAuthenticationError, authenticateWithPasskey } from "@emdash-cms/auth/passkey";

//#region src/astro/routes/api/auth/passkey/verify.ts
const prerender = false;
const POST = async ({ request, locals, session }) => {
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		const body = await parseBody(request, passkeyVerifyBody);
		if (isParseError(body)) return body;
		const url = new URL(request.url);
		const siteName = await new OptionsRepository(emdash.db).get("emdash:site_title") ?? void 0;
		const siteUrl = getPublicOrigin(url, emdash?.config);
		const passkeyConfig = getPasskeyConfig(url, siteName, siteUrl, validateAllowedOrigins(siteUrl, getConfiguredAllowedOrigins(emdash?.config)));
		const adapter = createKyselyAdapter(emdash.db);
		const challengeStore = createChallengeStore(emdash.db);
		const user = await authenticateWithPasskey(passkeyConfig, adapter, body.credential, challengeStore);
		if (session) session.set("user", { id: user.id });
		return apiSuccess({
			success: true,
			user: {
				id: user.id,
				email: user.email,
				name: user.name,
				role: user.role
			}
		});
	} catch (error) {
		if (error instanceof PasskeyAuthenticationError) return apiError("UNAUTHORIZED", "Authentication failed", 401);
		return handleError(error, "Authentication failed", "PASSKEY_VERIFY_ERROR");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=verify.mjs.map