CyberSearch .in
SUBSCRIBE
← All articles
WEB SECURITY · 7 min read

Adding passkeys and 2FA to your own site

Day 12 of our web security series: which second factors protect your users best, how passkeys work, and how to add them without locking people out.

Day 12 of Web Security, From the Ground Up: passkeys and 2FA for your own site

All week we’ve been making passwords harder to steal and harder to guess. But a password is still a single secret that can be phished, reused or leaked from another site. The strongest thing a site can do is stop relying on it alone.

That’s what two-factor login (2FA) and passkeys do. Today is about adding them to a site you run: which options are worth offering, how passkeys actually work, and how to roll them out without creating a support nightmare.

If you’re looking at this from the user’s side, our earlier post two-factor authentication, ranked compares the options you’ll meet as an account holder.

Why it matters for security

Every attack we covered on Day 9 (brute force, spraying and credential stuffing) ends the moment a correct password is no longer enough. Microsoft and Google have both reported that a second factor blocks the overwhelming majority of automated account takeovers. And passkeys go further: they can’t be phished at all, because there’s no secret for the user to type into the wrong site.

The options, from weakest to strongest

SMS codes

A six-digit code sent by text message. It’s far better than nothing and everyone knows how to use it. But texts can be intercepted, redirected by SIM swap fraud (an attacker convinces a mobile operator to move your number to their SIM), and typed into a convincing fake login page just as easily as a password. Offer it as a fallback if you must, not as the main option.

Authenticator apps (TOTP)

Apps like Google Authenticator, Microsoft Authenticator or a password manager generate a new six-digit code every 30 seconds from a secret shared once, usually by scanning a QR code. There’s no phone network involved, so SIM swaps don’t matter. It’s cheap to add: nearly every language has a well-maintained TOTP library.

The weakness is the same as SMS: a fake site can ask for the code and pass it on in real time. TOTP stops credential stuffing cold, but not a determined phishing attack.

Push approvals

“Is this you signing in?” with an Approve button on the phone. Convenient, but attackers learned to spam requests until a tired user taps Approve. If you build this, show a number on the login screen that the user has to match in the app, rather than a plain yes/no.

Passkeys and security keys

This is the strongest option, and the one to aim for. Let’s look at it properly.

How passkeys work

A passkey is built on a standard called WebAuthn. Instead of a shared secret, it uses a pair of keys:

  1. When the user creates a passkey, their device (phone, laptop or a hardware security key) makes a private key that never leaves it, and a matching public key it sends to your site.
  2. At login, your site sends a random challenge. The device asks the user to unlock it with a fingerprint, face or PIN, then signs the challenge with the private key.
  3. Your server checks the signature with the stored public key. If it matches, the user is in.

Three things make this so strong:

  • Nothing secret is stored on your server. A database leak exposes only public keys, which are useless to an attacker.
  • Nothing is typed, so nothing can be phished. The browser ties each passkey to your exact domain. A fake site at a look-alike address simply can’t ask for it.
  • It’s quicker for users. A fingerprint is faster than typing a password and a code.

Passkeys sync through the user’s Apple, Google or password manager account, so losing one phone doesn’t lose the passkey. Hardware security keys don’t sync, which is exactly why high-risk accounts like admins and finance staff often use them.

Building it without writing crypto

You should never implement the signature checks yourself. Use a maintained library for your language that handles WebAuthn registration and verification, or an identity provider that supports passkeys out of the box. Most popular frameworks and hosted login services now do. What’s left for you is the user experience around it.

Rolling it out well

The technology is the easy part. The hard part is the people.

Make it easy to turn on. Offer passkey or 2FA setup right after sign-up or a successful login, with one clear button. Don’t bury it three menus deep.

Plan for lost devices before launch. When someone loses their phone, how do they get back in? Give one-time recovery codes at setup and ask the user to save them. Encourage adding a second passkey or method. And make sure your account recovery process isn’t a weaker back door: on Day 11 we saw how a reset flow can undo every other protection.

Protect the settings page. Turning 2FA off, adding a new passkey or changing the account’s email should require the user to prove it’s them again, and should trigger a notification email. Otherwise an attacker with a stolen session (Day 10) can quietly remove the protection.

Store secrets carefully. TOTP secrets have to be kept on the server in a form that can be read back, so encrypt them, and keep the key apart from the database. Recovery codes should be hashed like passwords.

Rate limit the code step too. A six-digit code only has a million possibilities. Without the limits from Day 9, a script can work through them.

Require it where it matters most. For admin accounts, staff tools and anything that moves money, make a strong second factor mandatory, ideally passkeys or security keys. For ordinary users, offer it, encourage it, and make it the default for new accounts if you can.

What you can do today

If you use websites:

  1. Turn on a passkey wherever it’s offered, starting with your email, Google, Apple or Microsoft account and your password manager.
  2. Where passkeys aren’t available, choose an authenticator app over SMS.
  3. Save your recovery codes somewhere safe, such as your password manager or a printed copy at home.
  4. Never approve a login prompt you didn’t start, and never read a code out to someone who calls you.

If you run a website:

  1. Add an authenticator app option using a well-maintained TOTP library, and plan passkey support next.
  2. Use a maintained WebAuthn library or identity provider for passkeys. Never write the cryptography yourself.
  3. Issue recovery codes, rate limit code entry, and require re-authentication to change any security setting.
  4. Make a strong second factor mandatory for admin and staff accounts.
  5. Read the OWASP Multifactor Authentication Cheat Sheet and MDN’s Web Authentication API guide for implementation details.

Coming up next

On Day 13 we move from who someone is to what they’re allowed to do: getting permissions right, so a logged-in user can only see and change what’s theirs.

Written & tested by CyberSearch Get one tip a week →
KEEP READING

More from the blog

WEB SECURITYNov 16, 2026 · 7 min
Password reset flows that don't become a back door

Day 11 of our web security series: how the Forgot password link works, where it goes wrong, and how to build one that only lets the real owner back in.

WEB SECURITYNov 15, 2026 · 7 min
Sessions done right: secure cookie settings explained

Day 10 of our web security series: every setting on a login cookie explained, and the server-side habits that keep a logged-in user safe.

WEB SECURITYNov 14, 2026 · 7 min
Login pages that resist guessing: rate limits and lockouts

Day 9 of our web security series: how attackers try passwords at scale, and how a login page can slow them down without locking out the real user.