feat: two-factor sign-in, required for sensitive permissions
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:
1 parent
69c55a72fb
commit
d5bc792190
28 files changed
+4365
-62
No files matched your search
@@ -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>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -36,6 +36,7 @@ export default async function PersonPage({ params }: { params: Promise<{ id: str
|
||||
</span>
|
||||
{person.banned && <Tag tone="danger">banned: {person.banReason}</Tag>}
|
||||
{!person.approved && <Tag tone="strong">waiting for approval</Tag>}
|
||||
{person.twoFactor && <Tag>two-factor on</Tag>}
|
||||
</span>
|
||||
</PageHeader>
|
||||
<ConsoleNav />
|
||||
|
||||
@@ -65,7 +65,12 @@ export default async function People({
|
||||
{p.username}
|
||||
</Link>
|
||||
</TableCell>
|
||||
<TableCell className="capitalize">{p.role}</TableCell>
|
||||
<TableCell>
|
||||
<span className="capitalize">{p.role}</span>
|
||||
{p.role !== "user" && !p.twoFactor && (
|
||||
<span className="ml-2 text-xs text-muted-foreground">no two-factor</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="tabular-nums">{p.linkCap}</TableCell>
|
||||
<TableCell className="text-muted-foreground">
|
||||
<DateTime iso={p.createdAt} dateOnly />
|
||||
|
||||
Reference in new issue
Block a user