npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@clowk/hono

v1.0.2

Published

Hono middleware for Clowk authentication

Readme

@clowk/hono

Hono middleware for Clowk authentication. Extracts and verifies JWTs from incoming requests and sets the decoded user on c.get('auth').

Works on Cloudflare Workers, Bun, Deno, and Node.js.

Install

npm install @clowk/hono @clowk/core

Quick Start

import { Hono } from 'hono'
import { configure } from '@clowk/core'
import { clowkMiddleware, requireAuth } from '@clowk/hono'

configure({
  secretKey: process.env.CLOWK_SECRET_KEY,
})

const app = new Hono()

// Apply to all routes — sets c.get('auth') (or null)
app.use('*', clowkMiddleware())

// Public route — auth may be null
app.get('/', (c) => {
  const auth = c.get('auth')
  if (auth) {
    return c.json({ message: `Hello, ${auth.email}` })
  }
  return c.json({ message: 'Hello, visitor' })
})

// Protected route — returns 401 if no valid token
app.use('/api/*', requireAuth())

app.get('/api/me', (c) => {
  const auth = c.get('auth')
  return c.json({ user: auth })
})

export default app

API

clowkMiddleware(options?)

Creates middleware that extracts the JWT, verifies it, and sets the context variable auth.

app.use('*', clowkMiddleware({
  secretKey: 'sk_live_...',   // optional, defaults to config.secretKey
  tokenParam: 'token',        // query param name (default: "token")
  cookieKey: 'clowk_token',   // cookie name (default: "clowk_token")
}))

Token extraction order:

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

Behavior:

  • Valid token → c.get('auth') contains the decoded JWT payload
  • No token → c.get('auth') returns null, request continues
  • Invalid token → c.get('auth') returns null, request continues

requireAuth(options?)

Same as clowkMiddleware, but returns 401 Unauthorized if no valid token is present.

app.use('/api/*', requireAuth())

Response when unauthorized:

{ "error": "Unauthorized" }

Examples

Cloudflare Workers

import { Hono } from 'hono'
import { configure } from '@clowk/core'
import { clowkMiddleware, requireAuth } from '@clowk/hono'

type Bindings = {
  CLOWK_SECRET_KEY: string
}

const app = new Hono<{ Bindings: Bindings }>()

// Configure per-request (Workers have no global state)
app.use('*', async (c, next) => {
  configure({ secretKey: c.env.CLOWK_SECRET_KEY })
  await next()
})

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

app.get('/api/me', (c) => {
  const auth = c.get('auth')
  if (!auth) return c.json({ error: 'Unauthorized' }, 401)
  return c.json({ id: auth.sub, email: auth.email })
})

export default app

API with route groups

import { Hono } from 'hono'
import { configure } from '@clowk/core'
import { clowkMiddleware, requireAuth } from '@clowk/hono'

configure({ secretKey: process.env.CLOWK_SECRET_KEY })

const app = new Hono()

// Public routes
app.get('/health', (c) => c.json({ status: 'ok' }))

// Routes with optional auth
const publicApi = new Hono()
publicApi.use('*', clowkMiddleware())
publicApi.get('/posts', (c) => {
  const auth = c.get('auth')
  return c.json({ posts: getPosts(), viewer: auth?.sub ?? null })
})

// Protected routes
const protectedApi = new Hono()
protectedApi.use('*', requireAuth())
protectedApi.get('/me', (c) => {
  const auth = c.get('auth')!
  return c.json({ id: auth.sub, email: auth.email })
})
protectedApi.get('/settings', (c) => {
  const auth = c.get('auth')!
  return c.json({ settings: getSettings(auth.sub!) })
})

app.route('/api', publicApi)
app.route('/api', protectedApi)

export default app

OAuth callback handler

import { setCookie } from 'hono/cookie'

app.get('/clowk/oauth/callback', (c) => {
  const token = c.req.query('token')
  if (!token) return c.redirect('/sign-in?error=no_token')

  setCookie(c, 'clowk_token', token, {
    httpOnly: true,
    sameSite: 'Lax',
    secure: true,
    maxAge: 7 * 24 * 60 * 60, // 7 days
  })

  const returnTo = c.req.query('return_to') ?? '/'
  return c.redirect(returnTo)
})

TypeScript

The middleware extends Hono's context variable map:

declare module 'hono' {
  interface ContextVariableMap {
    auth: JwtPayload | null
  }
}

Access c.get('auth') with full type safety:

app.get('/api/me', (c) => {
  const auth = c.get('auth') // typed as JwtPayload | null
  if (!auth) return c.json({ error: 'Unauthorized' }, 401)
  return c.json({ id: auth.sub, email: auth.email })
})