Skip to content

toHono(handler)

Wraps the verified function into a Hono middleware.

Signature

ts
function toHono(
  handler: (req: Request) => VerificationState | Promise<VerificationState>,
): MiddlewareHandler

Parameters

handler

A function that takes a Request and returns a VerificationState (or a Promise of one). In practice this is always gate.verified from an x511() instance.

Behavior

State returned by handlerMiddleware 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 gate = x511({
  domain: 'https://example.com',
  basePath: '/x511',
  providers: ['self'],
  disclousures: { minAge: 18 },
})

// Internal routes - must be mounted before the protected routes
app.all('/x511/*', (c) => gate.verify(c.req.raw))

// Protected route
app.get(
  '/members',
  toHono(gate.verified), // <-- gate middleware
  (c) => c.text('Members only content'),
)

Using verified directly (without toHono)

If you are not using Hono, call gate.verified(req) directly and handle the state yourself:

ts
const state = gate.verified(request)

if (state.type === 'verified') {
  // proceed
} else {
  // state.type === 'pending'
  // Return 511 with your own page, or use buildPage(state)
}