Passwords remain the weakest component of web authentication. Users reuse credentials across breaches, attackers automate credential stuffing at scale, and even SMS or TOTP one-time codes can be intercepted by real-time reverse-proxy phishing kits. Passkeys—built on top of the Web Authentication API (WebAuthn) and FIDO2 standards—replace shared secrets with origin-bound public-key cryptography.
According to MDN Web Docs on WebAuthn, the API has been Baseline Widely available since September 2021, runs exclusively in secure HTTPS contexts, and cryptographically ties every credential to the Relying Party domain origin. Once you pair passkeys with solid session architecture—such as the trade-offs explored in JWT vs Sessions vs OAuth2—you eliminate password storage risks while giving users instant biometric or device PIN login.
Why Passkeys Outperform Passwords and SMS Two-Factor Codes
With password authentication, the server stores a hashed secret and compares user input on every sign-in. If an attacker tricks a user into typing their password and six-digit SMS code on a lookalike domain such as app-login-example.com, the proxy relays both credentials to the legitimate backend and hijacks the session.
WebAuthn prevents that attack structurally. During credential creation, the user authenticator (a phone biometric enclave, laptop TPM, password manager, or hardware security key) generates an asymmetric key pair. The private key never leaves the authenticator, while only the public key is saved by your backend—the Relying Party (RP) in W3C WebAuthn Level 2 terminology. Because the browser signs the exact origin URL inside clientDataJSON, a phishing site on any other domain cannot obtain a valid signature for your service.
| Authentication Factor | Server Stores Secret | Phishing Resistant | Vulnerable to Credential Stuffing |
|---|---|---|---|
| Password only | Yes (hashed) | No | Yes |
| Password + SMS / TOTP | Yes (shared secret) | No (AiTM proxies) | Partially |
| Passkey (WebAuthn) | No (public key only) | Yes (origin-bound) | No |
WebAuthn Registration Flow with navigator.credentials.create
Adding a passkey to a signed-in user account is called the registration ceremony. Your backend first generates a cryptographically random, single-use challenge buffer, stores it temporarily against the session with a short expiration window, and sends PublicKeyCredentialCreationOptions to the browser. The client converts binary fields such as challenge and user.id into ArrayBuffer instances and calls navigator.credentials.create().
typescriptinterface RegistrationOptionsJSON {
challenge: string;
rp: { name: string; id: string };
user: { id: string; name: string; displayName: string };
pubKeyCredParams: Array<{ type: "public-key"; alg: number }>;
}
function base64UrlToBuffer(value: string): ArrayBuffer {
const padded = value.replace(/-/g, "+").replace(/_/g, "/");
const binary = atob(padded);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i += 1) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
}
export async function registerPasskey(serverOpts: RegistrationOptionsJSON) {
const credential = (await navigator.credentials.create({
publicKey: {
rp: serverOpts.rp,
user: {
id: base64UrlToBuffer(serverOpts.user.id),
name: serverOpts.user.name,
displayName: serverOpts.user.displayName,
},
challenge: base64UrlToBuffer(serverOpts.challenge),
pubKeyCredParams: serverOpts.pubKeyCredParams,
authenticatorSelection: {
residentKey: "required",
userVerification: "preferred",
},
timeout: 60000,
},
})) as PublicKeyCredential | null;
if (!credential) {
throw new Error("Passkey creation was cancelled.");
}
return credential;
}
Notice two critical options above. Setting residentKey: "required" requests a discoverable credential so users can later sign in without typing a username first. Meanwhile, pubKeyCredParams typically advertises COSE algorithms -7 (ES256) and -257 (RS256) to ensure broad hardware and platform compatibility. For teams managing multiple login methods, check how passwordless flows coexist with social sign-in in our guide on JavaScript authentication and OAuth2 workflows.

Verifying Challenges, Origin Binding, and Attestation on the Server
Never roll your own CBOR or COSE signature parser in production—use a battle-tested server library such as @simplewebauthn/server, webauthn-rs, or Go’s go-webauthn. Regardless of the library you choose, understanding what the server must validate prevents subtle security gaps:
- Challenge equality and one-time consumption: Decode
response.clientDataJSON, verify thatchallengematches the exact random value issued for this ceremony, and immediately delete the challenge from Redis or your database so it cannot be replayed. - Strict origin and RP ID verification: Confirm
clientDataJSON.originequals your exacthttps://origin (no wildcard subdomains) and verify the SHA-256 hash of your Relying Party ID (rpIdHash) insideauthenticatorData. - User presence and verification flags: Assert that the User Present (UP) bit is set, and require the User Verified (UV) bit whenever your flow demands biometric or device PIN confirmation rather than silent presence.
- Public key storage and signCount checks: Persist the credential ID, extracted COSE public key, transports, and
signCountcounter, checking for anomalous counter regressions on non-synced authenticators.
Passkey Sign-In Using Discoverable Credentials and Conditional UI
The assertion ceremony authenticates an existing user via navigator.credentials.get(). Rather than forcing returning visitors onto a separate “Sign in with Passkey” screen, modern implementations leverage Conditional UI—standardized and documented in the passkeys.dev terminology reference. By adding autocomplete="username webauthn" to your email or username input and passing mediation: "conditional" on page load, the browser displays saved passkeys right inside the familiar autofill menu.
typescriptexport async function startConditionalPasskeyLogin(challengeBase64Url: string) {
if (!window.PublicKeyCredential?.isConditionalMediationAvailable) {
return null;
}
const available = await PublicKeyCredential.isConditionalMediationAvailable();
if (!available) {
return null;
}
const assertion = await navigator.credentials.get({
mediation: "conditional",
publicKey: {
challenge: base64UrlToBuffer(challengeBase64Url),
rpId: window.location.hostname,
userVerification: "preferred",
},
});
return assertion;
}
When the user taps their passkey from the autofill dropdown, the browser returns an assertion containing userHandle (the opaque user.id bytes you supplied during registration) plus a signature over authenticatorData and the SHA-256 hash of clientDataJSON. Your server looks up the matching credential record, verifies the cryptographic signature with the stored public key, and issues an HTTP-only session cookie.
Always use an opaque random identifier for WebAuthn user.id rather than an email address or integer primary key, so credentials never leak PII across authenticators.
Handling Synced vs Device-Bound Passkeys and Account Recovery
A frequent production surprise is assuming every passkey belongs to a single physical device. As passkeys.dev notes, modern platform ecosystems distinguish between synced passkeys—backed up across a user’s Apple, Google, or password-manager keychain—and device-bound passkeys stored solely on a security key or enterprise hardware module. Synced passkeys routinely report a constant signCount of zero, so your verification logic should never reject logins simply because signCount did not increment.
Cross-Device Authentication and Safe Recovery
What happens when a user logs in on a friend’s laptop or switches operating systems? Cross-device authentication (using a QR code and Bluetooth proximity) lets a phone verify the login for a desktop browser without copying the private key. Even so, account recovery remains essential: allow users to register multiple passkeys (for example, phone plus hardware backup key), label each credential with a readable device name and creation timestamp in account settings, and retain a verified fallback recovery path—such as email magic-link re-verification with step-up waiting periods for high-risk accounts.
Deploying passkeys incrementally alongside existing login flows gives users dramatically faster sign-ins while cutting phishing exposure immediately. Start with opt-in passkey enrollment inside account security settings, enable Conditional UI autofill on your login form, and monitor enrollment and assertion success rates before retiring legacy passwords.