"use client"; // Two-factor sign-in on the account page: turning it on (scan a code into an authenticator app, keep the backup codes, // confirm with a code), new backup codes, and turning it off. Sensitive permissions need it on. import { useRouter } from "next/navigation"; import { useState } from "react"; import { renderSVG } from "uqr"; import { CopyButton } from "@/components/copy-button"; import { ErrorText } from "@/components/forms"; import { Tag } from "@/components/status"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { authClient } from "@/lib/auth-client"; type Stage = | { kind: "idle" } | { kind: "password"; then: "enable" | "codes" | "disable" } | { kind: "setup"; uri: string; codes: string[] } | { kind: "codes"; codes: string[] }; export function TwoFactorCard({ enabled, neededFor }: { enabled: boolean; neededFor: string[] }) { const router = useRouter(); const [stage, setStage] = useState({ kind: "idle" }); const [error, setError] = useState(""); const [pending, setPending] = useState(false); async function run(fn: () => Promise) { setError(""); setPending(true); const failed = await fn(); if (failed) setError(failed); setPending(false); } async function withPassword(then: "enable" | "codes" | "disable", password: string) { if (then === "enable") { const { data, error } = await authClient.twoFactor.enable({ password }); if (error || !data || !("totpURI" in data) || !data.totpURI) return error?.message ?? "That didn't work. Try again."; setStage({ kind: "setup", uri: data.totpURI, codes: data.backupCodes ?? [], }); } else if (then === "codes") { const { data, error } = await authClient.twoFactor.generateBackupCodes({ password, }); if (error || !data) return error?.message ?? "That didn't work. Try again."; setStage({ kind: "codes", codes: data.backupCodes }); } else { const { error } = await authClient.twoFactor.disable({ password }); if (error) return error.message ?? "That didn't work. Try again."; setStage({ kind: "idle" }); router.refresh(); } } async function confirm(code: string) { const { error } = await authClient.twoFactor.verifyTotp({ code: code.replace(/\s+/g, ""), }); if (error) return error.message ?? "That code didn't work. Try again."; setStage({ kind: "idle" }); router.refresh(); } const submitTo = (fn: (form: FormData) => Promise) => (e: React.FormEvent) => { e.preventDefault(); const form = new FormData(e.currentTarget); void run(() => fn(form)); }; return ( Two-Factor Sign-In {enabled && on} Signing in asks for a code from an authenticator app on your phone as well as your password. {!enabled && neededFor.length > 0 && stage.kind !== "setup" && ( Turn this on to use everything you can do These need it:{" "} {neededFor.map((p, i) => ( {i > 0 && ", "} {p} ))} . )} {stage.kind === "idle" && (enabled ? (
) : ( ))} {stage.kind === "password" && (
withPassword(stage.then, String(f.get("password"))))} className="space-y-3">

{stage.then === "disable" ? neededFor.length > 0 ? "Turning it off stops the things listed above working until you turn it on again. Enter your password." : "Enter your password to turn it off." : stage.then === "codes" ? "Your old backup codes stop working. Enter your password." : "Enter your password to start."}

)} {stage.kind === "setup" && (
  1. Scan this with an authenticator app (such as Google Authenticator, Microsoft Authenticator or 1Password).

    Or enter this key:{" "} {new URL(stage.uri).searchParams.get("secret")}

  2. Keep these backup codes somewhere safe. Each signs you in once if you lose your phone.

  3. Enter the code the app shows.

    confirm(String(f.get("code"))))} className="flex max-w-md flex-wrap gap-2" >
)} {stage.kind === "codes" && (

Your new backup codes. Keep them somewhere safe: they won't be shown again.

)} {error && {error}}
); } function BackupCodes({ codes }: { codes: string[] }) { return (
    {codes.map((c) => (
  • {c}
  • ))}
); }