honojs-middleware/packages/clerk-auth
Yusuke Wada ee12e3c08a
chore: bump hono to `3.11.7` (#311)
2023-12-14 18:33:01 +09:00
..
src feat: Clerk Middleware (#151) 2023-10-27 16:50:22 +09:00
test feat: Clerk Middleware (#151) 2023-10-27 16:50:22 +09:00
CHANGELOG.md Version Packages (#213) 2023-10-27 16:58:49 +09:00
README.md chore(clerk-auth): Update README.md (#226) 2023-11-03 03:41:00 +09:00
jest.config.js feat: Clerk Middleware (#151) 2023-10-27 16:50:22 +09:00
package.json chore: bump hono to `3.11.7` (#311) 2023-12-14 18:33:01 +09:00
tsconfig.json feat: Clerk Middleware (#151) 2023-10-27 16:50:22 +09:00

README.md

Clerk middleware for Hono

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