feat(auth): support TOTP two-factor authentication for email/password users
Summary
Add opt-in TOTP (authenticator app) two-factor authentication for users signing in with email/password, plus admin-level enforcement toggles. Zero new infrastructure — no SMS provider, no email OTP — using Better Auth's built-in twoFactor plugin (already a transitive dep of better-auth).
Motivation
Paperclip is now multi-user (companies, invites, memberships), and many installs expose their auth surface to the public internet. Email/password without a second factor is the weakest widely-deployed auth mode. Today there's no way for a paperclip admin to add an account-security layer without forking. TOTP is the lowest-friction option: every major password manager and authenticator app supports it, and it requires no server-side notification infrastructure.
Proposed design
Per-user opt-in (v1):
- Settings → Security page: enable/disable 2FA, view backup codes, regenerate
- Enrollment wizard: password confirm → QR + secret → confirm TOTP → show 10 backup codes (once)
- `/2fa` verify page after sign-in; "Trust this device" supported
Admin enforcement:
- Instance-admin toggle: "Require 2FA for all users"
- Company-admin toggle: "Require 2FA for this company's members"
- Soft enforcement: on next sign-in, unenrolled users hit a forced enrollment screen
Explicitly out of scope (follow-up issues):
- Email OTP / SMS OTP (hooks left in place for a follow-up)
- WebAuthn / passkeys
- Recovery flows beyond backup codes
Implementation sketch
- `server/src/auth/better-auth.ts`: register `twoFactor({ issuer })` plugin
- Drizzle migration: `user.two_factor_enabled`, `two_factor` table (per Better Auth schema)
- Two settings keys for enforcement (instance + company)
- UI: `TwoFactorVerify` page at `/2fa`, `UserSecuritySettings` page at `/settings/security`, enrollment wizard component, enforcement gate in authenticated shell
- New UI dep: `qrcode` (~30KB, MIT) for QR rendering
~1,200 LOC, backwards-compatible, flags default off.
Willing to implement
Yes — will open a PR referencing this issue.
Source: paperclipai/paperclip