//#region src/auth/challenge-store.ts
function createChallengeStore(db) {
	return {
		async set(challenge, data) {
			const expiresAt = new Date(data.expiresAt).toISOString();
			await db.insertInto("auth_challenges").values({
				challenge,
				type: data.type,
				user_id: data.userId ?? null,
				data: null,
				expires_at: expiresAt
			}).onConflict((oc) => oc.column("challenge").doUpdateSet({
				type: data.type,
				user_id: data.userId ?? null,
				expires_at: expiresAt
			})).execute();
		},
		async get(challenge) {
			const row = await db.selectFrom("auth_challenges").selectAll().where("challenge", "=", challenge).executeTakeFirst();
			if (!row) return null;
			const expiresAt = new Date(row.expires_at).getTime();
			if (expiresAt < Date.now()) {
				await this.delete(challenge);
				return null;
			}
			return {
				type: row.type === "registration" ? "registration" : "authentication",
				userId: row.user_id ?? void 0,
				expiresAt
			};
		},
		async delete(challenge) {
			await db.deleteFrom("auth_challenges").where("challenge", "=", challenge).execute();
		}
	};
}
/**
* Clean up expired challenges.
* Should be called periodically (e.g., on startup, or via cron).
*/
async function cleanupExpiredChallenges(db) {
	const now = (/* @__PURE__ */ new Date()).toISOString();
	const result = await db.deleteFrom("auth_challenges").where("expires_at", "<", now).executeTakeFirst();
	return Number(result.numDeletedRows ?? 0);
}

//#endregion
export { createChallengeStore as n, cleanupExpiredChallenges as t };
//# sourceMappingURL=challenge-store-anhwVER0.mjs.map