Biometric Authentication for Web Apps
In this article
Yes, you can add biometric passwordless authentication to any web app using the Web Authentication (WebAuthn) API, which enables secure, phishing-resistant login via built-in biometrics like fingerprint, face recognition, or a security key. This method replaces passwords with public-key cryptography and is natively supported in modern browsers. Before starting, ensure your web app runs over HTTPS, as WebAuthn requires a secure context.
What You'll Need
- A web app served over HTTPS (required for WebAuthn)
- A modern browser that supports WebAuthn (Chrome, Firefox, Edge, Safari)
- Access to your server-side backend to handle registration and authentication logic
- A development environment with Node.js or another backend language (e.g., Python, PHP)
- A client-side JavaScript environment
- A testing device with biometric support (e.g., laptop with fingerprint reader, smartphone with Face ID or Touch ID)
Step-by-Step
- Set up HTTPS locally for development using tools like
mkcertor a service likengrokto create a secure tunnel. WebAuthn will not work on HTTP, even on localhost. - Include the
webauthn-jsonlibrary or use native browser APIs directly. This library simplifies encoding and decoding of WebAuthn data into JSON-friendly formats. - On the server, generate a unique user ID and challenge (a cryptographically random byte string) when a user begins registration. The challenge prevents replay attacks.
- Use the
navigator.credentials.create()method on the client to start registration. Pass apublicKeyobject containing:challenge: the server-generated challengerp(relying party): your domain and name (e.g., {"name": "MyApp", "id": "example.com"})user: user ID, name, and display namepubKeyCredParams: list of supported public key algorithms (e.g., "alg": -7 for ECDSA with SHA-256)attestation: set to "none" for most use cases
- After the user confirms with their biometric sensor, the browser returns a public key credential. Send this to your server.
- On the server, verify the registration response using a WebAuthn library (e.g.,
simplewebauthn/serverfor Node.js). This checks the attestation signature, validates the origin, and stores the credential’s public key and credential ID against the user. - For login, generate a new challenge and send known credentials (credential IDs) associated with the user to the client.
- Call
navigator.credentials.get()with apublicKeyobject containing the challenge and allowed credentials. The browser prompts the user to authenticate via biometrics. - Send the authentication response to the server and verify it using the same WebAuthn library. This checks the signature against the stored public key and ensures the challenge matches.
- Upon successful verification, establish a session (e.g., set a secure, HTTP-only cookie) and log the user in.
Common Mistakes
- Attempting to use WebAuthn over HTTP: it only works on HTTPS, including localhost with valid certificates.
- Reusing challenges: each challenge must be unique and short-lived to prevent replay attacks.
- Not validating the
rpIdand origin on the server: attackers can spoof responses from other domains. - Storing private keys: the private key never leaves the user’s device. Only store the public key and credential ID.
- Ignoring user verification requirements: set
userVerificationto "required" if you need to enforce biometric confirmation, not just presence.
Tips
- Use a well-maintained WebAuthn library like
simplewebauthnto avoid implementing cryptographic checks manually. - Support both registration and authentication fallbacks, such as passkeys or traditional login, for users without biometric devices.
- Store credential IDs and public keys securely in your database, associating them with user accounts.
- Set timeout values on challenges to enhance security; typically 60 seconds is sufficient.
- Test across devices: biometric support varies between desktops, phones, and browsers.
Troubleshooting
If registration fails, first check that the site is served over HTTPS. Then verify that the challenge is correctly formatted (Base64URL-encoded) and not reused. If the browser does not prompt for biometrics, ensure the device supports it and that the user has enrolled a method. On mobile, some browsers may fall back to device unlock rather than a dedicated biometric prompt. If authentication fails, confirm that the credential ID sent by the client matches one stored for the user and that the signature verification passes. Use browser developer tools to inspect the credential response and ensure no CORS or mixed content issues are blocking the request. Clear stored credentials during development using the browser’s settings if testing repeatedly.
Frequently Asked Questions
Does WebAuthn work on all browsers?
Yes, all major modern browsers support WebAuthn, including Chrome, Firefox, Edge, and Safari. Support began around 2018 and is now widely available.
Can I use WebAuthn without a backend?
No. WebAuthn requires server-side validation of registration and authentication responses to verify cryptographic signatures and prevent spoofing.
Is biometric data stored on the server?
No. Only public keys and credential IDs are stored. Biometric data never leaves the user’s device and is not accessible to your app.
What happens if a user gets a new phone?
They must re-register their biometric credentials on the new device. Each device generates unique key pairs, so old credentials won’t work.
Can I use WebAuthn for enterprise logins?
Yes. WebAuthn is ideal for enterprise use, especially with security keys or passkeys that support roaming credentials across devices.
Do users need to enroll biometrics first?
Yes. The user must have set up a biometric method (fingerprint, face scan) in their device OS before WebAuthn can use it.