trails/apps/journal/app/routes/api.auth.register.ts
Ullrich Schäfer e61179ab27 Implement notifications + supporting fixes
Adds the notifications system end-to-end (4 types, payload-versioned
JSONB, SSE-based live unread badge, /notifications page, mark-read API,
fan-out job for activity_published, daily 90-day retention purge).
Bell icon in the navbar with unread badge.

Side-findings from exercising the change:
- Add 6-digit magic code to registration (mirrors login UX, mobile
  paste-friendly), with `[Register Magic Link]` console line in dev so
  the code is reachable without a real email transport.
- Manual passkey/magic-link toggle on the register form (login already
  had it).
- Restrict ALPN to http/1.1 in HTTPS dev so React Router's
  singleFetchAction CSRF check (Origin vs. Host) passes — Node doesn't
  synthesize Host from h2's :authority. Plain HTTP dev unaffected.
- Followers/Following routes now use the locked-account rule from the
  profile route (owner + accepted followers see the list; others 404).
  Profile page renders the count chips as plain spans for viewers who
  can't see the lists, so private profiles don't surface dead links.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 01:28:55 +02:00

67 lines
2.9 KiB
TypeScript

import { data } from "react-router";
import type { Route } from "./+types/api.auth.register";
import { startRegistration, finishRegistration, createSession, addPasskeyStart, addPasskeyFinish, registerWithMagicLink } from "~/lib/auth.server";
import { sendWelcome, sendMagicLink } from "~/lib/email.server";
import { logger } from "~/lib/logger.server";
export async function action({ request }: Route.ActionArgs) {
const body = await request.json();
const { step, email, username, response, challenge, userId, termsAccepted, termsVersion } = body;
const origin = process.env.ORIGIN ?? `http://localhost:3000`;
// Registration steps require terms acceptance + the version the client
// agreed to (stored for audit so we can tell which text the user saw).
const requiresTerms = step === "start" || step === "finish" || step === "register-magic-link";
if (requiresTerms && !termsAccepted) {
return data({ error: "Terms of Service must be accepted" }, { status: 400 });
}
if (requiresTerms && (typeof termsVersion !== "string" || termsVersion.length === 0)) {
return data({ error: "Terms of Service version missing" }, { status: 400 });
}
try {
if (step === "start") {
const result = await startRegistration(email, username);
return data({ step: "challenge", options: result.options, userId: result.userId });
}
if (step === "finish") {
const newUserId = await finishRegistration(userId, email, username, response, challenge, termsVersion);
const cookie = await createSession(newUserId, request);
sendWelcome(email, username).catch((err) =>
logger.error({ err }, "Failed to send welcome email"),
);
return data({ step: "done" }, { headers: { "Set-Cookie": cookie } });
}
if (step === "register-magic-link") {
const { token, code } = await registerWithMagicLink(email, username, termsVersion);
const link = `${origin}/auth/verify?token=${token}`;
if (process.env.NODE_ENV !== "production") {
// Mirror the login endpoint so devs can grab either the link or
// the 6-digit code straight from the terminal.
console.log(`[Register Magic Link] ${email}: ${link} (code: ${code})`);
return data({ step: "magic-link-sent", devLink: link, code });
}
await sendMagicLink(email, link, code);
sendWelcome(email, username).catch((err) =>
logger.error({ err }, "Failed to send welcome email"),
);
return data({ step: "magic-link-sent" });
}
if (step === "add-passkey") {
const options = await addPasskeyStart(userId);
return data({ step: "challenge", options });
}
if (step === "finish-add-passkey") {
await addPasskeyFinish(userId, response, challenge);
return data({ step: "done" });
}
return data({ error: "Invalid step" }, { status: 400 });
} catch (e) {
return data({ error: (e as Error).message }, { status: 400 });
}
}