* docs(contributing): add section on testing Fixes #994 * test(clerk-auth): replace jest with vitest * test(graphql-server): replace jest with vitest * test(medley-router): replace jest with vitest * test(oauth-providers): replace jest with vitest * test(oidc-auth): replace jest with vitest * test(sentry): replace jest with vitest * test(trpc-server): replace jest with vitest * test(typebox-validator): replace jest with vitest * test(typia-validator): replace jest with vitest * test(valibot-validator): replace jest with vitest * chore: cleanup remaining references to jest * chore: fix lint errors * test(typia-validator): fix exclude directory * test(oauth-providers): use vitest environment miniflare * chore: update `yarn.lock` * chore(zod-openapi): pin zod-validator to 0.4.2 * test(graphql-server): remove console filtering --------- Co-authored-by: Yusuke Wada <yusuke@kamawada.com> |
||
---|---|---|
.. | ||
src | ||
CHANGELOG.md | ||
README.md | ||
package.json | ||
tsconfig.json | ||
vitest.config.ts |
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