- Move auth logic from page actions to dedicated API routes (/api/auth/register, /api/auth/login) — React Router page actions don't handle JSON fetch() properly - Remove server imports from page components - Verify registration start works end-to-end with PostgreSQL: WebAuthn challenge generated, user ID created, RP set to localhost Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import { data } from "react-router";
|
|
import type { Route } from "./+types/api.auth.login";
|
|
import { startAuthentication, finishAuthentication, createMagicToken, createSession } from "~/lib/auth.server";
|
|
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
const body = await request.json();
|
|
const { step, response, challenge, email } = body;
|
|
|
|
try {
|
|
if (step === "start-passkey") {
|
|
const options = await startAuthentication();
|
|
return data({ step: "challenge", options });
|
|
}
|
|
|
|
if (step === "finish-passkey") {
|
|
const userId = await finishAuthentication(response, challenge);
|
|
const cookie = await createSession(userId, request);
|
|
return data({ step: "done" }, { headers: { "Set-Cookie": cookie } });
|
|
}
|
|
|
|
if (step === "magic-link") {
|
|
const token = await createMagicToken(email);
|
|
const link = `${process.env.ORIGIN ?? "http://localhost:3000"}/auth/verify?token=${token}`;
|
|
console.log(`[Magic Link] ${email}: ${link}`);
|
|
return data({ step: "magic-link-sent" });
|
|
}
|
|
|
|
return data({ error: "Invalid step" }, { status: 400 });
|
|
} catch (e) {
|
|
return data({ error: (e as Error).message }, { status: 400 });
|
|
}
|
|
}
|