Passwords fail in two predictable ways. They leak: a breach at any service where a user recycled the same password hands attackers a credential that automated tools will try against the user’s other accounts. And they can be phished, because a password is a shared secret the user can be tricked into typing into the wrong site. Passkeys eliminate both failure modes by removing the shared secret entirely. This guide explains what a passkey is under the hood, walks through the WebAuthn registration and authentication ceremonies, and implements both with @simplewebauthn/server, the library most Node.js teams reach for.
A passkey is a FIDO2 credential: an asymmetric key pair created for one specific site. The private key is generated inside the user’s authenticator — a phone, a laptop’s secure enclave, or a hardware security key — and it never leaves that authenticator. Your server stores only the public key, which is worthless to an attacker who steals your database. At sign-in, the authenticator proves it holds the private key by signing a fresh challenge. There is no secret to type, reuse, or phish.
1. What a Passkey Is — and the Two Kinds You Will Meet
Every passkey is scoped to a single Relying Party — your site, identified by its RP ID (covered in section 4). The authenticator generates a brand-new key pair per RP, and using it requires local user verification: a fingerprint, a face scan, or a device PIN. That gesture unlocks the private key for one signature; the biometric itself never leaves the device either.
You will encounter two flavours. Synced passkeys are backed up — end-to-end encrypted — through the user’s platform account (iCloud Keychain, Google Password Manager, and similar), so the same passkey appears on all their devices and survives a lost phone. Device-bound passkeys never leave the single authenticator that created them, as with most hardware security keys. Your verification code handles both identically; the difference shows up in the backup flags and, crucially, in how you design account recovery.
2. The Two Ceremonies, Step by Step
Registration happens once per device or account. Step 1. An already signed-in user clicks “Add a passkey”. Your server generates a random challenge and the options describing your RP and the user, and stores the challenge in the session. Step 2. The browser calls navigator.credentials.create(); the authenticator generates the key pair, asks the user to verify with biometrics or PIN, and returns a credential ID plus an attestation object containing the new public key. Step 3. Your server verifies the attestation: the challenge must match the one it issued, the origin and RP ID must be yours, and the attestation signature must validate. Step 4. You store the credential ID, the public key, the initial sign counter, and the transports, linked to the user’s account.
Authentication is the everyday login. Step 1. Your server generates a fresh challenge. Step 2. The browser calls navigator.credentials.get(); the authenticator finds the credential it holds for your RP ID, the user verifies, and it signs the authenticator data together with a hash of the challenge, incrementing its sign counter. Step 3. Your server verifies the signature with the stored public key, re-checks challenge, origin, and RP ID, and confirms the new counter is greater than the stored one. Step 4. You update the stored counter and create the session. No password crosses the network at any point.
3. Implementation with @simplewebauthn/server
Install @simplewebauthn/server on the backend and @simplewebauthn/browser on the frontend. The browser package’s startRegistration({ optionsJSON }) and startAuthentication({ optionsJSON }) wrap the raw navigator.credentials calls and handle the base64url conversions for you. Two rules govern everything below: the challenge is generated server-side, stored in the session, and consumed exactly once; and verification always happens server-side. Registration first:
import {
generateRegistrationOptions,
verifyRegistrationResponse,
} from '@simplewebauthn/server';
const rpID = 'example.com';
const origin = 'https://example.com';
app.get('/passkey/registration-options', async (req, res) => {
const user = req.currentUser; // user is already signed in
const options = await generateRegistrationOptions({
rpName: 'DevInsights',
rpID,
userID: new TextEncoder().encode(user.id),
userName: user.email,
attestationType: 'none',
excludeCredentials: user.passkeys.map((p) => ({ id: p.credentialID })),
});
req.session.currentChallenge = options.challenge;
res.json(options);
});
app.post('/passkey/verify-registration', async (req, res) => {
const verification = await verifyRegistrationResponse({
response: req.body,
expectedChallenge: req.session.currentChallenge,
expectedOrigin: origin,
expectedRPID: rpID,
});
if (!verification.verified || !verification.registrationInfo) {
return res.status(400).json({ verified: false });
}
const { credential } = verification.registrationInfo;
await db.passkeys.create({
data: {
userId: user.id,
credentialID: credential.id,
publicKey: Buffer.from(credential.publicKey).toString('base64url'),
counter: credential.counter,
transports: credential.transports ?? [],
},
});
res.json({ verified: true });
});What you store is small: the credential ID (base64url, unique — it is how a login attempt finds the right row), the public key, the sign counter, and the transports (so the browser can hint whether the credential lives on a phone, a security key, or both). Add a user foreign key, a friendly device name, and created/last-used timestamps for the management screen. Authentication mirrors registration:
app.get('/passkey/authentication-options', async (req, res) => {
const options = await generateAuthenticationOptions({ rpID });
req.session.currentChallenge = options.challenge;
res.json(options);
});
app.post('/passkey/verify-authentication', async (req, res) => {
const passkey = await db.passkeys.findByCredentialID(req.body.id);
if (!passkey) return res.status(400).json({ verified: false });
const verification = await verifyAuthenticationResponse({
response: req.body,
expectedChallenge: req.session.currentChallenge,
expectedOrigin: origin,
expectedRPID: rpID,
credential: {
id: passkey.credentialID,
publicKey: Buffer.from(passkey.publicKey, 'base64url'),
counter: passkey.counter,
transports: passkey.transports,
},
});
if (!verification.verified) {
return res.status(400).json({ verified: false });
}
await db.passkeys.updateCounter(
passkey.credentialID,
verification.authenticationInfo.newCounter,
);
req.session.userId = passkey.userId; // log the user in
res.json({ verified: true });
});4. RP ID and Origin: Why Phishing Simply Fails
The RP ID is a domain string — typically your registrable domain, such as example.com. It must equal the host of the page’s origin, or be a registrable suffix of it: a page on login.example.com may use RP ID example.com or login.example.com, but never other.com, and a page on example.com cannot claim login.example.com. The origin — scheme, host, and port — is reported by the browser, not by page JavaScript, and WebAuthn requires a secure context (HTTPS, or localhost for development).
This binding is the phishing defence. The browser and authenticator will only create or use a credential when the current origin matches the RP ID the credential was made for. A pixel-perfect copy of your login page on examp1e.com has a different RP ID, so the victim’s authenticator simply has no credential to offer it — there is nothing to type and nothing to steal. Your server-side checks of expectedOrigin and expectedRPID are the second lock: they reject any assertion produced for a different origin or RP.
5. UX: Roll Out Passkeys Without Locking Anyone Out
Treat passkeys as an addition, not a flag day. The highest-converting pattern is a prompt immediately after a successful password login — “Create a passkey for faster, safer sign-in?” — when the user is already authenticated and the value is obvious. Give users a security settings page where they can see their passkeys, name them (“iPhone”, “YubiKey”), add more, and revoke them; allow several per account, because people own several devices.
On the login form, use conditional UI: start an authentication request with autofill mediation enabled and autocomplete="username webauthn" on the username field, so the browser offers the passkey right inside the familiar autofill menu instead of forcing users to find a special button. And build recovery before you encourage anyone to go passwordless: verified-email recovery, backup codes, or a support flow with strong identity checks. Synced passkeys make “new phone” a non-event for most consumers, but recovery is what stands between a device-bound user and permanent lockout.
Common Mistakes to Avoid
Not persisting — or never checking — the sign counter. The counter is your clone detector: store it at registration and overwrite it with newCounter on every successful login. If a device-bound authenticator ever reports a counter that fails to increase, treat it as a cloned-credential warning. (Synced passkeys commonly report 0 forever; log those rather than hard-rejecting your mobile user base.) Skipping origin and RP ID verification. Passing the wrong expectedOrigin, or accepting verified without the library having checked them, quietly deletes the phishing resistance you adopted passkeys for. Trying to store the private key. It never exists on your server — not encrypted, not hashed, not at all. If your schema has a column for it, the design has misunderstood the protocol; you store the public key and the credential ID, full stop. Removing the password fallback before recovery exists. A user whose only credential was device-bound and whose key is at the bottom of a lake is now your support ticket of the month. Keep a fallback until recovery is built, tested, and staffed.
Frequently Asked Questions (FAQ)
Q: What happens if the user loses their phone?
If the passkey was synced, it reappears on the replacement phone as soon as the user signs in to their platform account (Apple or Google) and passes that account’s own checks. If it was device-bound — a security key, say — it is gone with the device, and the user falls back to whatever recovery you built: a second passkey, backup codes, or your support flow. This is why accounts should encourage at least two passkeys.
Q: Are passkeys supported in all browsers?
WebAuthn is supported in all modern browsers — Chrome, Edge, Safari, and Firefox, on desktop and mobile — and platform passkey sync is available across Apple, Google, and Microsoft ecosystems. What still varies is UI polish and cross-device flows, so feature-detect with PublicKeyCredential checks and keep the password path available rather than sniffing user agents.
Q: Can passkeys be the only login method?
Technically yes, and some consumer services now run passkey-only. But the moment you remove passwords, account recovery becomes your product’s hardest problem: every lost-device case lands on it. Most production systems keep passkeys as the primary, promoted method with a recovery-grade fallback behind them — the configuration we recommend until your recovery flow has proven itself.
Conclusion
Passkeys replace a shared secret with a scoped key pair: the private key never leaves the authenticator, your database holds only public keys, and the RP ID binding makes phishing mechanically impossible rather than discouraged. The implementation is small — two ceremonies, one table, and disciplined verification of challenge, origin, RP ID, and counter. Roll it out next to passwords, invest in recovery early, and you remove your most-breached credential store without locking users out.
💡 Engineering Key Takeaway
Store public keys, never secrets: generate a fresh challenge per ceremony, verify origin, RP ID, and signature on the server, and persist the sign counter on every single login.