Javascript

WebAuthn requires JavaScript to interact with authenticators in the browser. This page explains the client-side implementation using the WebAuthn Browser API and recommended libraries.

Browser API Overview

The WebAuthn specification provides two main JavaScript APIs:

  1. navigator.credentials.create() - Register a new authenticator (attestation)

  2. navigator.credentials.get() - Authenticate with a registered authenticator (assertion)

Both APIs are asynchronous and return Promises that resolve with credential objects.

HTTPS Requirement

Modern browsers enforce this requirement to prevent credential theft and man-in-the-middle attacks.

@simplewebauthn/browser

We highly recommend using @simplewebauthn/browser. This library:

  • Simplifies the WebAuthn API with easy-to-use functions

  • Handles base64url encoding/decoding automatically

  • Provides TypeScript type definitions

  • Is fully compliant with the WebAuthn specification

  • Works with any server implementation

Installation:

Symfony UX Stimulus Controller

If you're using Symfony, the Stimulus Controller provides seamless integration with forms and authentication workflows without writing JavaScript code.

Registration (Attestation) Flow

Using Native Browser API

Using @simplewebauthn/browser

Authentication (Assertion) Flow

Using Native Browser API

Using @simplewebauthn/browser

Base64URL Encoding/Decoding

If you're using the native API without a library, you need helper functions for base64url encoding:

The @simplewebauthn/browser library handles all encoding/decoding automatically, which is why we recommend it.

Error Handling

WebAuthn operations can fail for various reasons. Always wrap calls in try-catch blocks:

Common Error Types

Error Name
Description
Common Cause

NotAllowedError

User cancelled the operation or timeout

User cancelled prompt, 60-second timeout exceeded

InvalidStateError

Invalid state for operation

Authenticator already registered for this account

NotSupportedError

Operation not supported

Browser doesn't support WebAuthn or specific features

SecurityError

Security requirements not met

Not using HTTPS, invalid origin/RP ID

AbortError

Operation aborted

Explicitly cancelled by application code

Browser Compatibility

WebAuthn is supported in modern browsers:

  • ✅ Chrome 67+

  • ✅ Firefox 60+

  • ✅ Safari 13+ (iOS and macOS)

  • ✅ Edge 18+

  • ✅ Opera 54+

Always check for WebAuthn support before attempting registration or authentication:

Browser Autofill / Conditional UI

Modern browsers support autofill for WebAuthn credentials, allowing users to select passkeys from password autofill:

For Symfony UX users, enable this with the useBrowserAutofill option in the Stimulus controller configuration.

Development Setup

Local HTTPS

For development with HTTPS on localhost:

Option 1: Use mkcert (Recommended)

Option 2: Symfony CLI (for Symfony projects)

The Symfony CLI automatically sets up HTTPS for local development.

See Also

Last updated

Was this helpful?