toHono(handler)
Wraps the verified function into a Hono middleware.
Signature
ts
function toHono(
handler: (req: Request) => VerificationState | Promise<VerificationState>,
): MiddlewareHandlerParameters
handler
A function that takes a Request and returns a VerificationState (or a Promise of one). In practice this is always verified from an x511() instance.
Behavior
State returned by handler | Middleware behavior |
|---|---|
{ type: 'verified' } | Calls next() - the request proceeds to the route handler |
{ type: 'pending', ... } | Responds with HTTP 511 and the HTML verification page |
The 511 HTML page is generated by buildPage(state), which inlines provider-specific data (QR links, session ID) and UnoCSS styles.
Example
ts
import { Hono } from 'hono'
import { x511, toHono } from 'x511-tba'
const app = new Hono()
const { verify, verified } = x511({
domain: 'https://example.com',
basePath: '/x511',
providers: ['self'],
disclousures: { minAge: 18 },
})
// Internal routes - must be mounted before the protected routes
app.mount('/x511', verify)
// Protected route
app.get('/members', toHono(verified), (c) => c.text('Members only content'))Using verified directly (without toHono)
If you are not using Hono, call verified(req) directly and handle the state yourself:
WARNING
The request must be a native Request instance (WinterTC compatible).
ts
const state = verified(request)
if (state.type === 'verified') {
// proceed
} else {
// state.type === 'pending'
// Return 511 with your own page, or use buildPage(state)
}