Two cleanups in one pass: 1. Update import paths app-wide from `~/lib/auth.server` to `~/lib/auth/session.server` for the four session helpers (sessionStorage, createSession, getSessionUser, destroySession). ~40 files: 33 simple path swaps where the file imported only session symbols, 5 splits where it also imported per-method auth functions (auth.verify.tsx, api.settings.email.ts, activities.\$id.tsx, routes.\$id.tsx, auth.accept-terms.tsx) — those keep one import from auth.server (for verifyMagicToken, canView, recordTermsAcceptance, etc.) and gain a second import from auth/session.server. Two more files used relative paths and were missed by the first grep pass (lib/oauth.server.ts and routes/oauth.authorize.tsx) — migrated too. The @deprecated re-exports block in auth.server.ts is gone. 2. Rename the new auth files to follow the project's `.server.ts` convention so Vite/React Router treat them as server-only (they read process.env.SESSION_SECRET, hit the DB, etc. — must NOT enter the client bundle): - auth/session.ts → auth/session.server.ts - auth/completion.ts → auth/completion.server.ts - auth/completion.test.ts → auth/completion.server.test.ts Done with `git mv` so blame is preserved. Verified: typecheck + lint green; 126 unit tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
67 lines
2.9 KiB
TypeScript
67 lines
2.9 KiB
TypeScript
import { data } from "react-router";
|
|
import type { Route } from "./+types/api.auth.register";
|
|
import { startRegistration, finishRegistration, addPasskeyStart, addPasskeyFinish, registerWithMagicLink } from "~/lib/auth.server";
|
|
import { completeAuth } from "~/lib/auth/completion.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, returnTo } = 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);
|
|
sendWelcome(email, username).catch((err) =>
|
|
logger.error({ err }, "Failed to send welcome email"),
|
|
);
|
|
return completeAuth({ userId: newUserId, request, returnTo, mode: "json" });
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|