web-authn
Passkey register / authenticate with HTML. Pair it with Quark to render the result.
<web-authn start-method="register" options-url="/api/registration-options" verify-url="/api/users">
<form>
<button type="submit">One click sign up!</button>
</form>
</web-authn><web-authn start-method="register" options-url="/api/registration-options" verify-url="/api/users">
<form>
<button type="submit">One click sign up!</button>
</form>
</web-authn>Features
- Provides data Use Quark to render the verify response
- Full ceremony Fetches options, runs the browser's WebAuthn prompt, then verifies — one element
- Register or authenticate
start-methodpicks the ceremony - Submit command
--submitstarts the ceremony programmatically — for forms outside the DOM subtree, or buttons outside the<form> - Chainable
web-authn-successfires like any{tag}-successevent — chain a redirect or next step - Highly configurable Headers, credentials, redirect, etc
Installation
This package is available in the
<script src="https://unpkg.com/@excom/kit-utils@0.3.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/neutron@0.2.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/web-authn@0.1.5/dist/index.umd.min.js"></script>npm install @excom/web-authn<!-- import path to `node_modules` will depend on your build setup -->
<script type="module" src="/node_modules/@excom/web-authn"></script>
<link rel="stylesheet" href="/node_modules/@excom/web-authn">import "@excom/web-authn";Peer dependencies (0)
Packages a consumer must install alongside this one. Workspace deps are bundled.
| Package | Version |
|---|---|
|
Depends on @simplewebauthn/browser for the actual WebAuthn calls (startRegistration / startAuthentication). You can use any server-side library to handle the WebAuthn requests, but it is recommended to use the counterpart library, @simplewebauthn/server, since they seamlessly understand the same contract.
Usage
<web-authn options-url="/api/webauthn/register/options"
verify-url="/api/webauthn/register/verify" start-method="register">
<form>
<input name="username" required>
<button type="submit">Register passkey</button>
</form>
</web-authn><web-authn options-url="/api/webauthn/register/options"
verify-url="/api/webauthn/register/verify" start-method="register">
<form>
<input name="username" required>
<button type="submit">Register passkey</button>
</form>
</web-authn>On submit: options-url is fetched for ceremony options, the browser's native passkey prompt runs (@simplewebauthn/browser), and the resulting credential is posted to verify-url. Use start-method="authenticate" for sign-in instead of registration.
Hook the lifecycle state with CSS:
web-authn[is-loading] { /* show loading spinner */ }
web-authn[is-error]::before { content: "An error occurred." }web-authn[is-loading] { /* show loading spinner */ }
web-authn[is-error]::before { content: "An error occurred." }Or Quark:
web-authn[is-success] {
$res: prop("provision").body;
span { content: $res.verified; }
}web-authn[is-success] {
$res: prop("provision").body;
span { content: $res.verified; }
}Chain a next step off success the same way you would for any <super-form> or <provider-fetch>:
<event-handler listen-for="web-authn-success" fire-event="onboarding-step-complete">
<web-authn options-url="/api/webauthn/register/options"
verify-url="/api/webauthn/register/verify" start-method="register">
<form><input name="username"><button type="submit">Register</button></form>
</web-authn>
</event-handler><event-handler listen-for="web-authn-success" fire-event="onboarding-step-complete">
<web-authn options-url="/api/webauthn/register/options"
verify-url="/api/webauthn/register/verify" start-method="register">
<form><input name="username"><button type="submit">Register</button></form>
</web-authn>
</event-handler>Examples
Authenticate
API Reference
Attributes
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
Provision
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
Events
Type
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements
Child or descendant elements are recognized by and relevant to its functionality.Styles
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
web-authn
Attributes (15)
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
api-method
apiMethod
string
"GET"api-url
form-ref or custom doFetch() args) is merged in as query params instead.
apiUrl
string
""fetch-credentials
RequestInit.credentials mode.
fetchCredentials
string
"omit" | "same-origin" | "include""include"fetch-redirect
RequestInit.redirect mode. Unset defers to the browser default (follow).
fetchRedirect
string
"follow" | "error" | "manual"nullform-ref
<form> to intercept. Must be a descendant to be heard directly — point elsewhere and invoke the --submit command instead.
formRef
string
<CSS Selector>":scope form"form-ref
<form> to source the request from — its action (URL), method, enctype (Content-Type), and field values (as the JSON payload) all take priority over the matching attributes below. Omit to build the request entirely from attributes / custom doFetch() args.
formRef
string
<CSS Selector>nullhas-body
GET / HEAD). Already implied for POST / PUT / PATCH.
hasBody
boolean
falseheader-accept
Accept request header.
headerAccept
string
"application/json"header-cache-control
Cache-Control request header. Unset by default (browser default caching applies).
headerCacheControl
string
nullheader-content-type
Content-Type request header. Dropped entirely when the request has no body.
headerContentType
string
"application/json"start-method
startMethod
string
"register" | "authenticate"nullverify-url
verifyRegistrationResponse / verifyAuthenticationResponse). Receives the credential as the request body.
verifyUrl
string
<URL>nullis-error
AbortError). Fires with the error event.
isError
boolean
falseis-loading
isLoading
boolean
falseis-success
is-loading and is-error.
isSuccess
boolean
falseProvision (1)
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
provision
{ status, statusText, ok, headers, url, redirected, bodyUsed, type, body }. Failure shape is either that same response shape (server responded with an error status) or { message, stack } (request never completed). Not reflected as an attribute.
provision
FetchResponse
Events (6)
e.preventDefault() is not
synchronously called on the event.
Type
web-authn-error
event.detail is the error payload (see provision). Not dispatched for aborted requests.
Type
FetchableErrorEvent
web-authn-loading
Type
FetchableLoadingEvent
web-authn-submit
doFetch().
Type
WebAuthnSubmitEvent
doFetch([url, requestInit]) with the event's detail (the verify-url request).web-authn-success
event.detail is the parsed response (see provision).
Type
FetchableSuccessEvent
submitWebAuthnNativeSubmitEvent
<form> matched by form-ref; prevented, then starts the ceremony.
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
--submit<button command="--submit" commandfor="…">) — the only option when form-ref points to a form that isn't a descendant.
Recognized Elements (0)
Child or descendant elements are recognized by web-authn and relevant to its functionality.Styles (0)
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
Release notes (2)
0.1.4
- A failed ceremony or options request now clears is-loading and sets is-error with the message in provision
0.1.1
- Ship only dist (and declared extras) in the npm tarball; drop build logs, tests and sources
Release notes
e.preventDefault() is not
synchronously called on the event.
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.