# Passkeys

> The Passkeys are a passwordless authentication method based on public key cryptography.

## What are passkeys and how do they work?

The credentials based on the WebAuthn standard that replace passwords with a key pair stored on the user's device. Sign-in is confirmed with biometrics or a PIN, and the keys are bound to a domain, which protects against phishing.

- Topic: Security
- Type: technology

## Example

Wrong:

```js
const cred = await navigator.credentials.get({
  publicKey: {
    challenge: crypto.getRandomValues(new Uint8Array(32)),
  },
});
```

Right:

```js
const { challenge } = await fetch('/auth/challenge').then((r) => r.json());
const cred = await navigator.credentials.get({
  publicKey: { challenge: base64urlToBuffer(challenge) },
});
await sendToServerForVerification(cred);
```

The challenge comes from the server and is checked there once, so a captured response cannot be replayed.

## Common mistakes

- Generating the challenge in the browser, which makes replay attacks possible.

## Key points

- The challenge is random, created on the server and used only once.
- The server verifies the signature, origin and relying party ID, not only that a response arrived.
- The username field has `autocomplete='username webauthn'` for passkey autofill.
- Users can register several passkeys and have a recovery path if a device is lost.

## Interview questions

### Passkeys vs passwords: are passkeys more secure?

Yes. The private key never leaves the authenticator and is bound to the site's domain, so passkeys cannot be phished, reused or leaked in a server breach.

### What happens if I lose the device with my passkey?

Passkeys synced by a password manager come back on a new device. Otherwise the user signs in with another registered passkey or the site's recovery method.

### Do passkeys work on localhost?

Yes. Browsers treat localhost as a secure context, so WebAuthn works there, while other origins need HTTPS.

## Related cards

- [navigator.credentials](https://wonderium.dev/js/navigator-credentials.md)
- [Accessible Authentication](https://wonderium.dev/a11y/accessible-authentication.md)
- [HTTPS](https://wonderium.dev/web/https.md)
- [Encryption](https://wonderium.dev/engineering/encryption.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/passkeys
- Last updated: 2026-11-25
