Register a passkey
Passkey creation/registration is a three-step process:
- Authorised in your backend
- Created in your frontend
- Verified and linked to an account in your backend
Backend: authorise registration
Section titled “Backend: authorise registration”Your backend needs to authorise the passkey creation. It will generate a registrationToken for use by your frontend:
import { Passlock } from "@passlock/server";
// from your development tenancy settingsconst passlock = new Passlock({ tenancyId: "myTenancyId", apiKey: "myApiKey"});
const user = await getCurrentUser();
const result = await passlock.authorizePasskeyRegistration({ rpId: "example.com", // "localhost" for development userId: user.id, username: user.email, displayName: user.name,});
if (result.success) { // provide this to your frontend code // see frontend/register.ts below return result.registrationToken;}Send only the registrationToken to your frontend. Treat it as a short-lived bearer token for creating one passkey for the user.
Frontend: create the passkey
Section titled “Frontend: create the passkey”Your frontend code will obtain a registrationToken from your backend, and use it to trigger passkey registration on the device:
import { Passlock } from "@passlock/browser";
const passlock = new Passlock({ tenancyId: "myTenancyId" });
createPasskeyButton.addEventListener("click", async () => { // see backend/register.ts above const registrationToken = await fetchTokenFromBackend();
const result = await passlock.registerPasskey({ registrationToken });
if (result.success) { // see backend/register.ts below await submitCodeToBackend(result.value.code); }});The browser success payload includes both an id_token and a code. This quickstart guide uses the code exchange flow. Pass the code to your backend for verification.
Backend: verify the passkey
Section titled “Backend: verify the passkey”Exchange the code for details about the passkey:
import { Passlock } from "@passlock/server";
// from your development tenancy settingsconst passlock = new Passlock({ tenancyId: "myTenancyId", apiKey: "myApiKey"});
const user = await getCurrentUser();
const result = await passlock.exchangeCode({ code });
if (result.success) { // link the passkey to a local user account in your database await assignPasskeyToUser(user.id, result.value.authenticatorId);};