KScript docs
EN

Sign-in: KScript.Auth

Passkeys

A passkey signs someone in with their device's lock (a fingerprint, a face or a PIN) instead of a password. KScript supplies the browser code and the HTTP endpoints, so you only add buttons.

Accounts without passwords

To create accounts using passkeys from the first visit, run this in your project:

CommandWhat it does
kscript make auth --passkeysPasskey sign-up, sign-in and an account page.

Open /register and choose Create account with a passkey. KScript creates the account, a User record with no email or password, and starts its session after the device verifies the ceremony. Records the account owns can extend Ownable or hold a user: User relation. Every passkey added on /account signs in to that same user. Add a second passkey before removing the first. An account with a single passkey must keep it.

The generator sets http.auth.passkey_signup and uses the configured serve port for localhost. For a deployed app, generate with --origin https://accounts.example.com. Add --rp-id example.com if you want that parent domain to scope the credentials. Add --api for the authenticated identity endpoint. A signed-in user can issue tokens from the account page. This kit has no password registration, sign-in or reset routes.

Adding passkeys to password accounts

In a project made with kscript make auth, set the public origin in kscript.json and restart kscript serve. For the default local server, use the following setting.

JSON
{ "http": { "auth": { "passkey_origin": "http://localhost:8080" } } }

Sign in to an existing account, open /account and choose Add a passkey. Sign out, then choose Sign in with a passkey on /login. The browser asks you to unlock your passkey. Each successful sign-in starts a fresh KScript session, so existing auth:session routes and model forms work with it. You can enroll multiple passkeys and remove a lost one from the account page.

Passkey buttons on your own pages

The <sign-in-form>, <sign-up-form> and <passkey-list> components hold these buttons. For your own pages, put them on the sign-in and signed-in account pages respectively. KScript supplies the browser code and the HTTP endpoints.

HTML page
<button type="button" passkey="signup" passkey-redirect="/account">Create account with a passkey</button>
<button type="button" passkey="login" passkey-redirect="/account">Sign in with a passkey</button>
<button type="button" passkey="register">Add a passkey</button>

Use @has_passkeys() from KScript.Auth to show the controls when the site has enabled them. After sign-in the browser returns to the protected URL that prompted it, or to http.auth.home. The optional passkey-redirect overrides that destination with a page on the same origin. From a signed-in account page, @passkeys() lists that user's credential IDs and creation dates. Call @revoke_passkey($id) from a live action, as <passkey-list> does, or from a CSRF-protected POST to remove one.

Your own browser code

For a custom browser UI, POST {} as application/json to /_ks/passkeys/signup/options for a new account, /_ks/passkeys/register/options to add a credential, or /_ks/passkeys/login/options to sign in. Use the returned public-key options with the browser's credentials API. POST the resulting credential JSON to verify in the same path, from the same origin and session. Verification returns {"ok":true} on success and includes the destination in redirect for sign-in. Each challenge expires after five minutes and can be verified once, on the process that issued it. Restarting a server discards pending ceremonies. When using several workers, route both steps to the same worker. Accounts and completed credentials persist in the auth store.

Deploying passkeys

For deployment, set passkey_origin to the exact public HTTPS origin, including a non-default port. The relying party ID defaults to its hostname. To keep credentials scoped to a parent domain you control, set passkey_rp_id before enrolling users. Changing the relying party ID requires enrolling new credentials. Back up the _passkeys folder alongside your auth user data. Use @users()->delete(id) to remove a stored account and its passkeys. If a separate system manages your users, give each account an immutable ID that is never reused.

Passkeys in the database

Passkeys are kept in a SQLite file in the auth folder unless you say otherwise. With "passkeys": "database" they go in the project's database instead, in three tables (kscript_passkey_accounts, kscript_passkeys and kscript_passkey_users), so every server of the app shares them and they are backed up with the rest of your data:

JSON
{ "http": { "auth": { "passkeys": "database", "sessions": "database" } } }

@passkeys() gives each passkey's last_used_at too, and synced (true for one that is backed up, such as a passkey in a password manager).

A sign-up page can ask for a name: a passkey button inside a form with a name field sends it, and it becomes the account's name and the name the device shows for the passkey. A name must be 1 to 40 characters, with no line breaks or characters that reverse text.

Checking it's still them

Adding or removing a passkey changes how someone gets into their account, so it is worth knowing that the person at the keyboard is the owner, not someone at a computer they left signed in. With "confirm_within": "5m", those changes need a passkey check (or a sign-in) within the last five minutes; otherwise they are refused as Forbidden. A passkey="login" button on a signed-in page is the check: it confirms the same account and starts a fresh session, and another account's passkey is refused. @is_confirmed() says whether the last check is recent enough, so a page can show the button first:

HTML page
<if test="!@is_confirmed()"><button type="button" passkey="login">Confirm it's you</button></if>

Give @is_confirmed('1m') a shorter time before something that can't be undone, such as deleting the account. A passkey sign-in records how they signed in as amr (pop and mfa, as RFC 8176 names a user-verified passkey), and passkey_remember: true keeps a passkey sign-in for the remember time rather than the browser session.