honojs-middleware/packages/clerk-auth
Jonathan Haines 1fd8ebf9b6
feat(eslint-config): enable linting with type information (#1098)
2025-04-07 19:31:09 +09:00
..
src chore: Migrate workspaces that use `jest` to `vitest` (#998) 2025-03-04 22:00:28 +09:00
CHANGELOG.md Version Packages (#474) 2024-04-25 22:46:58 +09:00
README.md chore: add coverage badges (#1023) 2025-03-19 17:53:11 +09:00
package.json build: typescript project references (#1077) 2025-04-02 18:28:02 +09:00
tsconfig.build.json build: typescript project references (#1077) 2025-04-02 18:28:02 +09:00
tsconfig.json build: typescript project references (#1077) 2025-04-02 18:28:02 +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