feat: two-factor sign-in, required for sensitive permissions
Pull request / title (pull_request) Successful in 2s
CI / test (pull_request) Successful in 1m11s
CI / image (apps/api/Dockerfile, t5de-hub-api) (pull_request) Skipped
CI / image (apps/web/Dockerfile, t5de-hub-web) (pull_request) Skipped

Better Auth's twoFactor plugin: an authenticator app (TOTP) with 10 backup codes.
Turning it on, making new backup codes and turning it off are on the account page,
and signing in asks for a code once it's on. Sensitive permissions (product:remove,
person:ban, person:role, settings:edit, catalog:suspend, ...) answer 403
two_factor_required without it, API tokens included; the console reminds people
who need it. Better Auth's own admin endpoints are closed (404).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
dhkatzandClaude Opus 5.5 committed 2026-11-05 19:17:46 -08:00
1 parent 69c55a72fb
commit d5bc792190
28 files changed
+4365 -62

No files matched your search

+49 -13
View File
@@ -1,7 +1,16 @@
import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { cache } from "react";
import { NavLink } from "@/components/header-client";
import { api, getMe, isModerator, unwrap, type ConsoleData, type Person } from "@/lib/api";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import {
api,
getMe,
isModerator,
unwrap,
type ConsoleData,
type Person,
} from "@/lib/api";
/** The signed-in moderator, or off to sign in; others get not found (these pages aren't for them). */
export async function requireModerator(path: string): Promise<Person> {
@@ -22,7 +31,9 @@ const loadConsole = cache(async (): Promise<ConsoleData | undefined> => {
* The signed-in person and their console, or off to sign in. People who manage nothing get not found: the console
* is for those controlling a product, catalog or room.
*/
export async function requireConsole(path: string): Promise<{ me: Person; console: ConsoleData }> {
export async function requireConsole(
path: string,
): Promise<{ me: Person; console: ConsoleData }> {
const me = await getMe();
if (!me) redirect(`/signin?next=${encodeURIComponent(path)}`);
const data = await loadConsole();
@@ -43,18 +54,43 @@ const tabs: { section: Section; href: string; label: string }[] = [
{ section: "audit", href: "/console/audit", label: "Audit" },
];
/** The console's sections this person may see. */
/** The console's sections this person may see, and a reminder when sensitive actions need two-factor sign-in. */
export async function ConsoleNav() {
const sections = new Set<Section>((await loadConsole())?.sections ?? ["overview"]);
const data = await loadConsole();
const sections = new Set<Section>(data?.sections ?? ["overview"]);
const needed = data?.twoFactorNeededFor ?? [];
return (
<nav className="mb-8 flex gap-1 border-b pb-3 text-sm">
{tabs
.filter((t) => sections.has(t.section))
.map((t) => (
<NavLink key={t.section} href={t.href} exact={t.section === "overview"}>
{t.label}
</NavLink>
))}
</nav>
<>
{needed.length > 0 && (
<Alert className="mb-6">
<AlertTitle>Turn on two-factor sign-in</AlertTitle>
<AlertDescription>
<span>
Some of what you can do here needs it ({needed.join(", ")}).{" "}
<Link
href="/account"
className="text-foreground underline underline-offset-4"
>
Turn it on in your account
</Link>
.
</span>
</AlertDescription>
</Alert>
)}
<nav className="mb-8 flex gap-1 border-b pb-3 text-sm">
{tabs
.filter((t) => sections.has(t.section))
.map((t) => (
<NavLink
key={t.section}
href={t.href}
exact={t.section === "overview"}
>
{t.label}
</NavLink>
))}
</nav>
</>
);
}