Files
kai 0f07d9032c
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m14s
release / tag and release (push) Successful in 2s
Data model, two-household test, backup (26.11.3) (#4)
2026-11-06 20:09:44 +00:00

64 lines
3.0 KiB
TypeScript

import type { RegistrationResponseJSON } from "@simplewebauthn/server";
import { withAuth } from "@/lib/db";
import { fail, isSecure, json, readJson } from "@/lib/auth/http";
import { challengeBody } from "@/lib/auth/schemas";
import { startSession } from "@/lib/auth/session";
import { removeSetupCode } from "@/lib/auth/codes";
import { deviceLabel, loadInstance, takeChallenge, verifyRegistration } from "@/lib/auth/webauthn";
import { cookies } from "next/headers";
import { resolveLocale, LOCALE_COOKIE } from "@/lib/locale";
export const dynamic = "force-dynamic";
type SetupContext = { memberId: string; householdId: string; name: string; household: string; origin: string; rpId: string };
// Einrichtung, Schritt 2: Passkey prüfen, dann Box, Haushalt, erste Person,
// Passkey und Sitzung in einer Transaktion anlegen.
export async function POST(request: Request) {
const body = challengeBody.safeParse(await readJson(request));
if (!body.success) return fail("invalid", 400);
const result = await withAuth(async (client) => {
if (await loadInstance(client)) return fail("alreadySetUp", 409);
const taken = await takeChallenge<SetupContext>(client, body.data.challengeId, "setup");
if (!taken) return fail("expired", 400);
const ctx = taken.context;
const instance = { origin: ctx.origin, rpId: ctx.rpId };
const passkey = await verifyRegistration(instance, taken.challenge, body.data.response as unknown as RegistrationResponseJSON).catch(
() => null,
);
if (!passkey) return fail("passkey", 400);
const locale = resolveLocale((await cookies()).get(LOCALE_COOKIE)?.value, request.headers.get("accept-language"));
await client.query("select set_config('app.household_id', $1, true)", [ctx.householdId]);
await client.query("insert into instance (origin, rp_id) values ($1, $2)", [instance.origin, instance.rpId]);
await client.query("insert into households (id, name) values ($1, $2)", [ctx.householdId, ctx.household]);
await client.query("insert into members (id, household_id, display_name, role, locale) values ($1, $2, $3, 'admin', $4)", [
ctx.memberId,
ctx.householdId,
ctx.name,
locale,
]);
await client.query("update households set created_by = $1 where id = $2", [ctx.memberId, ctx.householdId]);
await client.query(
`insert into passkeys (id, member_id, household_id, public_key, counter, transports, device_type, backed_up, label)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
[
passkey.id,
ctx.memberId,
ctx.householdId,
passkey.publicKey,
passkey.counter,
passkey.transports,
passkey.deviceType,
passkey.backedUp,
deviceLabel(request.headers.get("user-agent")),
],
);
await startSession(client, { id: ctx.memberId, householdId: ctx.householdId, passkeyId: passkey.id }, request, isSecure(instance.origin));
return null;
});
if (result) return result;
await removeSetupCode();
return json({ ok: true });
}