SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

Add passkeys

Enroll a passkey for a signed-in account and sign in with it, verifying both WebAuthn ceremonies on the server.

After reading this guide, you will know

  • How to enroll a passkey for a signed-in account
  • How to sign in with a passkey by verifying both WebAuthn ceremonies
  • How to keep the challenge between the two requests and store what the ceremony proves

A passkey is a key pair the person's device holds, bound to your domain, unlocked with a fingerprint, a face or a PIN. There is nothing to phish and nothing to leak from your database, because what you store is a public key. This guide adds one to a Remix v3 app: enrollment for an account that is already signed in, and a sign-in that needs no password.

@sdxc/passkey covers both halves. @sdxc/passkey/server issues ceremony options and verifies what the browser signs; @sdxc/passkey/client runs the ceremony in the browser, from a remix/component client entry. Every failure on either side is a @sdxc/result value. @sdxc/auth holds the session the challenge waits in, @sdxc/http answers the JSON, and @sdxc/ui draws the button.

Terminal
npm add @sdxc/auth @sdxc/http @sdxc/passkey @sdxc/result @sdxc/ui remix

Declare the endpoints

Each ceremony is two requests: the server issues a challenge, the browser signs it, the server verifies the signature. WebAuthn is a browser API, so these are JSON endpoints a client island talks to rather than form submissions.

A sign-in runs both requests around the authenticator's prompt, and enrollment has the same shape on the register routes:

Passkey sign-in: the browser island requests a challenge the server keeps in the session, the authenticator signs an assertion, the island posts it to verify, and the server spends the challenge, verifies and records the passkey, regenerates the session with the user id and returns a redirect URLPOST /passkeys/sign-in/challengekeepChallengeOptionsPasskey.authenticate(options)Signed assertionPOST /passkeys/sign-in/verifyspendChallengePasskeys.find, verifyAuthentication,Passkeys.recordUseregenerateId and set userIdRedirect URLlocation.assign(redirect)Browser islandBrowser islandServerServerSessionSessionAuthenticatorAuthenticator
routes/passkeys.ts
import { post, route } from "remix/routes";

export default route({
	register: {
		challenge: post("/passkeys/register/challenge"),
		verify: post("/passkeys/register/verify"),
	},
	signIn: {
		challenge: post("/passkeys/sign-in/challenge"),
		verify: post("/passkeys/sign-in/verify"),
	},
});

Nest it under passkeys in your route table in routes/web.ts, beside the dashboard a sign-in lands on, so the controllers below read routes.passkeys.signIn.verify and so on.

Create one relying party

The relying party holds your policy and nothing else, so one instance at module scope serves every request.

app/initializers/passkeys.ts
import { RelyingParty } from "@sdxc/passkey/server";
import { env } from "cloudflare:workers";

export const RELYING_PARTY = new RelyingParty({
	id: env.PASSKEY_RP_ID,
	name: "Acme",
	origin: env.APP_ORIGIN,
	userVerification: "required",
});

id is the registrable domain credentials are bound to, such as example.com; use the parent domain when one passkey must cover several subdomains. origin lists where a ceremony may run. userVerification: "required" turns a missing biometric or PIN into a verification failure, which is what you want when the passkey is the only thing a sign-in asks for.

Keep the challenge between the two requests

The challenge is issued by one request and checked by the next, so it has to outlive the first. The session is the smallest place that holds it — the one from Sign in with OpenID Connect works as it is.

app/services/auth/passkey-challenge.ts
import type { RequestContext } from "remix/router";

import { sessionOf } from "@sdxc/auth/remix/context";

const CHALLENGE_KEY = "passkey:challenge";

export function keepChallenge(ctx: RequestContext, challenge: string): void {
	sessionOf(ctx).set(CHALLENGE_KEY, challenge);
}

export function spendChallenge(ctx: RequestContext): string | null {
	let session = sessionOf(ctx);
	let challenge = session.get(CHALLENGE_KEY);
	session.unset(CHALLENGE_KEY);
	return typeof challenge === "string" ? challenge : null;
}

sessionOf from @sdxc/auth reads the session Remix's session middleware put on the context, and spendChallenge drops the value in the same call that reads it.

Store what the ceremony proves

Registration answers the row to store, as a RegisteredPasskey, and authentication later needs its id, publicKey and counter back. Keep it in a table of your own, beside the account id and a suspended flag; the controllers below reach it through a small Passkeys model of yours.

Enroll a passkey

Enrollment runs for somebody already signed in, because a passkey belongs to an account. That is also what makes it trustworthy: the account was authenticated when it enrolled, the challenge was fresh, and the origin and relying party id matched.

app/http/controllers/passkeys/register-challenge.ts
import { ok } from "@sdxc/http/response/json";
import { createAction } from "remix/router";

import { currentAccount } from "~/app/services/auth/current-account";
import { keepChallenge } from "~/app/services/auth/passkey-challenge";
import { RELYING_PARTY } from "~/app/initializers/passkeys";
import { Passkeys } from "~/app/models/passkeys";
import routes from "~/routes/web";

export default createAction(routes.passkeys.register.challenge, async (ctx) => {
	let account = await currentAccount(ctx);
	let { challenge, options } = RELYING_PARTY.register({
		user: { id: account.id, name: account.email },
		exclude: await Passkeys.listIds(ctx.db, account.id),
	});

	keepChallenge(ctx, challenge);
	return ok(options);
});

exclude lists the credentials the account already has, so the browser refuses to enroll the same device twice. currentAccount is your own helper answering the signed-in account.

The verify endpoint hands the request straight to the relying party, which reads and validates the JSON body itself:

app/http/controllers/passkeys/register-verify.ts
import { badRequest, ok } from "@sdxc/http/response/json";
import { isFailure } from "@sdxc/result";
import { createAction } from "remix/router";

import { currentAccount } from "~/app/services/auth/current-account";
import { spendChallenge } from "~/app/services/auth/passkey-challenge";
import { RELYING_PARTY } from "~/app/initializers/passkeys";
import { Passkeys } from "~/app/models/passkeys";
import routes from "~/routes/web";

export default createAction(routes.passkeys.register.verify, async (ctx) => {
	let challenge = spendChallenge(ctx);
	if (!challenge) return badRequest({ error: "rejected" });

	let result = await RELYING_PARTY.verifyRegistration(ctx.request, { challenge });
	if (isFailure(result)) {
		ctx.log.warn("passkey.registration_rejected", { reason: result.error.name });
		return badRequest({ error: "rejected" });
	}

	let account = await currentAccount(ctx);
	await Passkeys.create(ctx.db, { accountId: account.id, ...result.data });
	return ok({ enrolled: true });
});

Sign in with it

Leaving allow out of authenticate() makes the ceremony usernameless: the browser offers every passkey it holds for your domain, so nobody types an identifier first.

app/http/controllers/passkeys/sign-in-challenge.ts
import { ok } from "@sdxc/http/response/json";
import { createAction } from "remix/router";

import { keepChallenge } from "~/app/services/auth/passkey-challenge";
import { RELYING_PARTY } from "~/app/initializers/passkeys";
import routes from "~/routes/web";

export default createAction(routes.passkeys.signIn.challenge, (ctx) => {
	let { challenge, options } = RELYING_PARTY.authenticate();
	keepChallenge(ctx, challenge);
	return ok(options);
});

Verification needs the stored credential before there is anything to verify against, and the assertion names it by id, so this endpoint reads the body itself and hands the parsed response to the relying party:

app/http/controllers/passkeys/sign-in-verify.ts
import { sessionOf } from "@sdxc/auth/remix/context";
import { badRequest, ok } from "@sdxc/http/response/json";
import { CounterError } from "@sdxc/passkey/server";
import { isFailure, wrap } from "@sdxc/result";
import { createAction } from "remix/router";

import { spendChallenge } from "~/app/services/auth/passkey-challenge";
import { RELYING_PARTY } from "~/app/initializers/passkeys";
import { Passkeys } from "~/app/models/passkeys";
import routes from "~/routes/web";

export default createAction(routes.passkeys.signIn.verify, async (ctx) => {
	let challenge = spendChallenge(ctx);
	let body = await wrap(
		() => ctx.request.json() as Promise<AuthenticationResponseJSON>,
	);
	if (!challenge || isFailure(body)) return badRequest({ error: "rejected" });

	let passkey = await Passkeys.find(ctx.db, body.data.id);
	if (!passkey || passkey.suspended) return badRequest({ error: "rejected" });

	let result = await RELYING_PARTY.verifyAuthentication(body.data, {
		challenge,
		passkey,
	});
	if (isFailure(result)) {
		if (result.error instanceof CounterError)
			await Passkeys.suspend(ctx.db, passkey.id);
		ctx.log.warn("passkey.rejected", { reason: result.error.name });
		return badRequest({ error: "rejected" });
	}

	await Passkeys.recordUse(ctx.db, passkey.id, result.data.counter);
	let session = sessionOf(ctx);
	session.regenerateId?.();
	session.set("userId", passkey.accountId);
	return ok({ redirect: routes.dashboard.href() });
});

The cast only names the browser's shape: verifyAuthentication validates the whole response, and wrap catches a body that is not JSON. Every refusal answers the same body and is logged by error name, so a caller never learns which check failed while your logs do. userId stands for whichever key your app's session marks a signed-in account with.

Run the ceremony in the browser

The browser half is the one piece of the page that ships JavaScript. Keep the ceremony in a plain function: fetch the options, prompt, post the signed response, follow the redirect.

resources/components/passkey-ceremony.ts
import { CancelledError, Passkey } from "@sdxc/passkey/client";
import { isFailure } from "@sdxc/result";

export async function signInWithPasskey(challengeUrl: string, verifyUrl: string) {
	let issued = await fetch(challengeUrl, { method: "POST" });
	let options = (await issued.json()) as PublicKeyCredentialRequestOptionsJSON;

	let result = await Passkey.authenticate(options);
	if (isFailure(result)) {
		return result.error instanceof CancelledError ? null : result.error.message;
	}

	let verified = await fetch(verifyUrl, {
		method: "POST",
		headers: { "content-type": "application/json" },
		body: JSON.stringify(result.data),
	});
	if (!verified.ok) return "That passkey was not accepted.";

	let { redirect } = (await verified.json()) as { redirect: string };
	location.assign(redirect);
	return null;
}

A CancelledError means the person dismissed the prompt, which deserves silence rather than an error message. The island wraps it in a button and shows whatever message comes back:

resources/components/passkey-sign-in.tsx
import type { Handle } from "remix/component";

import { Button } from "@sdxc/ui";
import { clientEntry, on } from "remix/component";

import { signInWithPasskey } from "./passkey-ceremony";

type PasskeySignInProps = { challengeUrl: string; verifyUrl: string; label: string };

export const PasskeySignIn = clientEntry(
	import.meta.url,
	function PasskeySignIn(handle: Handle<PasskeySignInProps>) {
		let message: string | null = null;

		async function start() {
			let { challengeUrl, verifyUrl } = handle.props;
			message = await signInWithPasskey(challengeUrl, verifyUrl);
			void handle.update();
		}

		return () => (
			<>
				<Button type="button" mix={[on("click", () => void start())]}>
					{handle.props.label}
				</Button>
				{message && <p role="alert">{message}</p>}
			</>
		);
	},
);

Render it beside your other sign-in options, passing routes.passkeys.signIn.challenge.href() and routes.passkeys.signIn.verify.href() as the two URLs. Enrollment is the same shape with Passkey.register in place of Passkey.authenticate; there, an AlreadyRegisteredError means this device is already enrolled, so point the person at signing in instead.

Test the endpoints

A real assertion needs an authenticator, so the router test covers what the server decides alone: the options it issues, and a verify that arrives with no challenge in the session:

app/http/controllers/passkeys/sign-in.test.ts
import { expect, test } from "vitest";

import { createWebRouter } from "~/app/initializers/router";
import routes from "~/routes/web";

function post(href: string, body?: unknown) {
	return createWebRouter().fetch(
		new Request(new URL(href, "https://example.com"), {
			method: "POST",
			headers: { "content-type": "application/json" },
			body: body === undefined ? undefined : JSON.stringify(body),
		}),
	);
}

test("a sign-in starts with a challenge that requires verification", async () => {
	let response = await post(routes.passkeys.signIn.challenge.href());

	expect(response.status).toBe(200);
	expect(await response.json()).toMatchObject({
		challenge: expect.any(String),
		userVerification: "required",
	});
});

test("an assertion with no challenge in the session is rejected", async () => {
	let response = await post(routes.passkeys.signIn.verify.href(), {
		id: "credential",
		rawId: "credential",
		type: "public-key",
		response: {},
	});

	expect(response.status).toBe(400);
	expect(await response.json()).toEqual({ error: "rejected" });
});

The second request carries no session cookie, so spendChallenge finds nothing and the endpoint refuses before it reads a credential.

Where to go next

Documentation

Can’t find it? Contact us