Unofficial Clerk middleware for H3.
Check here for a demo with Nuxt.
To use this middleware you should first create a Clerk application and retrieve a Secret Key
and a Publishable Key
for your application (see here) to be used as environment variables CLERK_PUBLISHABLE_KEY
& CLERK_SECRET_KEY
.
npm install h3-clerk
import { createApp, createError, eventHandler } from 'h3'
import { clerkClient, getAuth, withClerkAuth, withClerkMiddleware } from 'h3-clerk'
const app = createApp()
// For all routes
app.use(withClerkMiddleware())
app.use('/protected-endpoint', async (event) => {
const { userId } = getAuth(event)
if (!userId)
throw createError({ statusCode: 403 })
const user = await clerkClient.users.getUser(userId)
return { user }
})
// For a specific route
app.use(
'/protected-endpoint',
withClerkAuth(async (event) => {
const { userId } = getAuth(event)
if (!userId)
throw createError({ statusCode: 403 })
const user = await clerkClient.users.getUser(userId)
return { user }
})
)
Name | Type | Description |
---|---|---|
authorizedParties |
string[] |
Validate that the azp claim in the Clerk Session JWT equals any of your known origins that are permitted to generate those tokens. This is an extra security check that we highly recommend that you do. For more information, refer to Manual JWT Verification. E.g. ['http://localhost:4003', 'https://clerk.dev'] |
jwtKey |
string |
Clerk's JWT session token can be verified in a networkless manner using the JWT verification key. By default, Clerk will use our well-known JWKs endpoint to fetch and cache the key for any subsequent token verification. If you use the CLERK_JWT_KEY environment variable or the jwtKey option to supply the key, Clerk will pick it up and do networkless verification for session tokens using it. For more information, refer to Networkless Token Verification. |
onError |
(error: ClerkAPIResponseError) => unknown |
This function can act as a custom error handler tailored to the needs of your application. |
import type { SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal'
declare module 'h3' {
interface H3EventContext {
auth: SignedInAuthObject | SignedOutAuthObject
}
}
MIT