Adding 2FA to Your Own App: A Developer’s Guide
Short answer: use a maintained TOTP library, encrypt secrets at rest, allow ±1 time step, rate-limit hard, hash your backup codes, and require the password to change 2FA settings. Then add WebAuthn — it fixes what TOTP structurally can’t.