@clowk/hono

Hono middleware for Clowk authentication

Install

npm install @clowk/hono @clowk/core

Overview

Hono middleware that verifies JWTs and sets the authenticated user on the context. Works on every runtime Hono supports: Cloudflare Workers, Bun, Deno, and Node.js.

What it provides:

  • clowkAuth() — middleware factory
  • c.get('clowkUser') — the decoded user after verification

clowkAuth

import { clowkAuth } from '@clowk/hono'

app.use('*', clowkAuth())

Options

OptionTypeDefaultDescription
onError(err, c) => ResponseReturns 401 JSONCustom error handler
extractToken(c) => string | nullAuto-detectCustom token extractor

Token extraction order

The middleware looks for the JWT in this order:

  1. Query parameter: ?token=eyJ...
  2. Cookie: clowk_token
  3. Authorization header: Bearer eyJ...

Context variables

After successful verification:

app.get('/me', (c) => {
  const user = c.get('clowkUser')
  // => { id, email, name, provider, iat, exp }

  return c.json({ user })
})

User type

interface ClowkUser {
  id: string
  email: string
  name: string
  provider: string
  iat: number
  exp: number
}

Error handling

By default, invalid or missing tokens return:

{ "error": "Unauthorized" }

Customize with onError:

const auth = clowkAuth({
  onError: (err, c) => {
    return c.json({ error: err.message }, 401)
  },
})

Runtimes

RuntimeDeploy target
Cloudflare Workerswrangler deploy
Bunbun run src/index.ts
Denodeno run src/index.ts
Node.jsnode src/index.ts

The @clowk/core package uses native fetch and Web Crypto APIs — no Node.js-specific dependencies.

Next steps

On this page