honojs-middleware/packages/clerk-auth
Jonathan Haines 9235709060
refactor: composite build (#1230)
* refactor: composite build

* chore(ua-blocker): move demo.ts out of src
2025-06-16 11:23:47 +09:00
..
src refactor(clerk-auth): enable isolated declarations (#1194) 2025-06-09 18:11:11 +09:00
CHANGELOG.md Version Packages (#1206) 2025-06-09 19:05:17 +09:00
README.md chore: add coverage badges (#1023) 2025-03-19 17:53:11 +09:00
package.json Version Packages (#1206) 2025-06-09 19:05:17 +09:00
tsconfig.build.json refactor: composite build (#1230) 2025-06-16 11:23:47 +09:00
tsconfig.json refactor: composite build (#1230) 2025-06-16 11:23:47 +09:00
tsconfig.spec.json feat(eslint-config): enable linting with type information (#1098) 2025-04-07 19:31:09 +09:00
vitest.config.ts test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00

README.md

Clerk middleware for Hono

codecov

This is a Clerk third-party middleware for Hono.

This middleware can be used to inject the active Clerk session into the request context.

Installation

npm i hono @hono/clerk-auth @clerk/backend

Configuration

Before starting using the middleware you must set the following environment variables:

CLERK_SECRET_KEY=<You-secret-key>
CLERK_PUBLISHABLE_KEY=<Your-publishable-key>

How to Use

import { clerkMiddleware, getAuth } from '@hono/clerk-auth'
import { Hono } from 'hono'

const app = new Hono()

app.use('*', clerkMiddleware())
app.get('/', (c) => {
  const auth = getAuth(c)

  if (!auth?.userId) {
    return c.json({
      message: 'You are not logged in.',
    })
  }

  return c.json({
    message: 'You are logged in!',
    userId: auth.userId,
  })
})

export default app

Accessing instance of Backend API client

import { clerkMiddleware, getAuth } from '@hono/clerk-auth'
import { Hono } from 'hono'

const app = new Hono()

app.use('*', clerkMiddleware())
app.get('/', async (c) => {
  const clerkClient = c.get('clerk')

  try {
    const user = await clerkClient.users.getUser('user_id_....')

    return c.json({
      user,
    })
  } catch (e) {
    return c.json(
      {
        message: 'User not found.',
      },
      404
    )
  }
})

export default app

Author

Vaggelis Yfantis https://github.com/octoper