{"id":4013,"date":"2026-10-27T06:01:02","date_gmt":"2026-10-26T23:01:02","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/10\/27\/how-to-implement-two-factor-authentication-in-your-web-app\/"},"modified":"2026-10-27T06:01:03","modified_gmt":"2026-10-26T23:01:03","slug":"how-to-implement-two-factor-authentication-in-your-web-app","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/10\/27\/how-to-implement-two-factor-authentication-in-your-web-app\/","title":{"rendered":"How to Implement Two-Factor Authentication in Your Web App"},"content":{"rendered":"<h1>How to Implement Two-Factor Authentication in Your Web App<\/h1>\n<p>Two-factor authentication (2FA) adds a second proof of identity beyond a password, blocking most account takeover attacks. This tutorial walks through a practical TOTP-based implementation you can ship in an afternoon.<\/p>\n<p>You will need a backend framework, a database, and one library: <code>otplib<\/code> for Node, <code>pyotp<\/code> for Python, or <code>rotp<\/code> for Ruby.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/sumberlaba.com\/wp-content\/uploads\/2026\/10\/article-1793055656672.jpg\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Step 1: Choose Your 2FA Method<\/h2>\n<p>Time-based one-time passwords (TOTP) work with any authenticator app, cost nothing to send, and resist SIM-swap attacks. Prefer them over SMS unless accessibility requirements demand otherwise.<\/p>\n<h2>Step 2: Handle Enrollment<\/h2>\n<p>When a user enables 2FA, generate a random base32 secret, encrypt it against the user record, and render it as a QR code plus a manual text key.<\/p>\n<ul>\n<li>Generate the secret with <code>authenticator.generateSecret()<\/code><\/li>\n<li>Build an <code>otpauth:\/\/<\/code> URI with issuer and account labels<\/li>\n<li>Do not activate 2FA until the user confirms one valid code<\/li>\n<\/ul>\n<h2>Step 3: Verify at Login<\/h2>\n<p>After a correct password, mark the session as &#8220;pending 2FA&#8221; and require a six-digit code. Verify with a one-step window to tolerate clock drift, then promote the session to fully authenticated.<\/p>\n<h2>Step 4: Prevent Lockouts and Abuse<\/h2>\n<ul>\n<li>Generate ten single-use recovery codes and store them hashed<\/li>\n<li>Rate-limit verification attempts to about five per fifteen minutes<\/li>\n<li>Log every enable or disable event and email the user on changes<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Start with TOTP, confirm enrollment before activation, and always ship recovery codes. A modest amount of code buys a large jump in account security.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Implement Two-Factor Authentication in Your Web App Two-factor authentication (2FA) adds a second proof of identity beyond a password, blocking most account takeover attacks. This tutorial walks through a practical TOTP-based implementation you can ship in an afternoon. You will need a backend framework, a database, and one library: otplib for Node, pyotp &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":4012,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-non-category"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/4013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=4013"}],"version-history":[{"count":1,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/4013\/revisions"}],"predecessor-version":[{"id":4014,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/4013\/revisions\/4014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media\/4012"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=4013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=4013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=4013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}