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

+117 -32
View File
@@ -4,30 +4,56 @@ import { useRouter } from "next/navigation";
import { useState } from "react";
import { ErrorText } from "@/components/forms";
import { Button } from "@/components/ui/button";
import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import {
Field,
FieldDescription,
FieldGroup,
FieldLabel,
} from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { authClient } from "@/lib/auth-client";
/** Signs in with Better Auth, then goes to next (already checked to be a path on this site). */
/** Signs in with Better Auth (then a code, if two-factor sign-in is on), then goes to next (already checked to be a
* path on this site). */
export function SignInForm({ next }: { next: string }) {
const router = useRouter();
const [error, setError] = useState("");
const [pending, setPending] = useState(false);
// After the password, when two-factor sign-in is on: a code from the app, or a backup code.
const [step, setStep] = useState<"password" | "code" | "backup">("password");
const done = () => {
router.push(next);
router.refresh();
};
async function signIn(form: FormData) {
const { data, error } = await authClient.signIn.username({
username: String(form.get("username")),
password: String(form.get("password")),
});
if (error) return setError(error.message ?? "That didn't work. Try again.");
if (data && "twoFactorRedirect" in data && data.twoFactorRedirect)
return setStep("code");
done();
}
async function verify(form: FormData) {
const code = String(form.get("code")).replace(/\s+/g, "");
const { error } =
step === "code"
? await authClient.twoFactor.verifyTotp({ code })
: await authClient.twoFactor.verifyBackupCode({ code });
if (error)
return setError(error.message ?? "That code didn't work. Try again.");
done();
}
async function submit(form: FormData) {
setError("");
setPending(true);
const { error } = await authClient.signIn.username({
username: String(form.get("username")),
password: String(form.get("password")),
});
if (error) {
setError(error.message ?? "That didn't work. Try again.");
setPending(false);
return;
}
router.push(next);
router.refresh();
await (step === "password" ? signIn(form) : verify(form));
setPending(false);
}
return (
@@ -37,25 +63,84 @@ export function SignInForm({ next }: { next: string }) {
void submit(new FormData(e.currentTarget));
}}
>
<FieldGroup>
<Field>
<FieldLabel htmlFor="username">Username</FieldLabel>
<Input id="username" name="username" autoComplete="username" required autoFocus />
</Field>
<Field>
<FieldLabel htmlFor="password">Password</FieldLabel>
<Input id="password" name="password" type="password" autoComplete="current-password" required />
</Field>
{error && <ErrorText>{error}</ErrorText>}
<Field>
<Button type="submit" disabled={pending}>
{pending ? "Signing in…" : "Sign in"}
</Button>
<FieldDescription className="text-center">
No account? <Link href="/signup">Create one</Link>
</FieldDescription>
</Field>
</FieldGroup>
{step === "password" ? (
<FieldGroup>
<Field>
<FieldLabel htmlFor="username">Username</FieldLabel>
<Input
id="username"
name="username"
autoComplete="username"
required
autoFocus
/>
</Field>
<Field>
<FieldLabel htmlFor="password">Password</FieldLabel>
<Input
id="password"
name="password"
type="password"
autoComplete="current-password"
required
/>
</Field>
{error && <ErrorText>{error}</ErrorText>}
<Field>
<Button type="submit" disabled={pending}>
{pending ? "Signing in…" : "Sign in"}
</Button>
<FieldDescription className="text-center">
No account? <Link href="/signup">Create one</Link>
</FieldDescription>
</Field>
</FieldGroup>
) : (
<FieldGroup key={step}>
<Field>
<FieldLabel htmlFor="code">
{step === "code"
? "Code from your authenticator app"
: "Backup code"}
</FieldLabel>
<Input
id="code"
name="code"
required
autoFocus
autoComplete="one-time-code"
{...(step === "code"
? {
inputMode: "numeric",
pattern: "\\s*[0-9]{6}\\s*",
maxLength: 8,
}
: { maxLength: 32 })}
className="font-mono"
/>
</Field>
{error && <ErrorText>{error}</ErrorText>}
<Field>
<Button type="submit" disabled={pending}>
{pending ? "Checking…" : "Sign in"}
</Button>
<FieldDescription className="text-center">
<button
type="button"
className="underline underline-offset-4"
onClick={() => {
setError("");
setStep(step === "code" ? "backup" : "code");
}}
>
{step === "code"
? "Use a backup code instead"
: "Use your authenticator app instead"}
</button>
</FieldDescription>
</Field>
</FieldGroup>
)}
</form>
);
}