Create a Stytch User
If the user attempting to register isn’t yet associated with a Stytch
user_id, you’ll have to create a new Stytch User via the Create User endpoint. The resulting user_id will be used to register a new WebAuthn device.Register a WebAuthn device
To authenticate with WebAuthn, you first need to register a WebAuthn device.First, you’ll make a request to the Start WebAuthn registration endpoint. You need two fields for the request: a Stytch
user_id and your login page’s domain. When using built-in browser methods like navigator.credentials.create() and navigator.credentials.get(), as we will in this guide, you’ll also need to set the use_base64_url_encoding option to true.Next, you’ll create a public key using the public_key_credential_creation_options from the Start WebAuthn registration response and use that to call the browser’s built-in navigator.credentials.create() method.If the navigator.credentials.create() call is successful, pass the resulting public key credential into our Register WebAuthn endpoint.Here is simplified backend and frontend example code demonstrating the full WebAuthn registration flow:Authenticate WebAuthn
Now that user has an active WebAuthn registration, you can use it for authentication.First, you’ll make a request to the Start WebAuthn authentication endpoint. Similarly to in Step 2, when using built-in browser methods, you’ll need to set the
use_base64_url_encoding option to true.Next, you’ll create a public key using the public_key_credential_request_options from the Start WebAuthn authentication response and use that to call the browser’s built-in navigator.credentials.get() method.If the navigator.credentials.get() call is successful, pass the resulting public key credential into our Authenticate WebAuthn endpoint. If the Authenticate WebAuthn call succeeds, your user has successfully authenticated.Here is simplified backend and frontend example code demonstrating the full WebAuthn authentication flow:[Optional but recommended] Manually serialize the public key credentials
In the above registration and authentication steps, we serialized the public key credentials by simply calling Here’s updated frontend registration and authentication code that uses the above manual serialization methods instead of
credential.toJSON(). This works in most cases, but there are some known incompatibilities with certain password managers and the public key credential’s toJSON() method.To avoid these incompatibilities, you can manually serialize the public key credentials instead of calling credential.toJSON(). Public key credential serialization code will look something like this, where the SerializedAttestationCredential is used in the WebAuthn registration request and the SerializedAssertionCredential is used in the WebAuthn authentication request:toJSON():