āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/clerk/clerk-docs/reference/fastify/clerk-plugin ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
clerkPlugin()'
description: The clerkPlugin() function is a Fastify plugin that integrates Clerk's authentication into your application.
sdk: fastifyThe clerkPlugin() function is a Fastify plugin that integrates Clerk's authentication into your application. The function checks request cookies and headers for a session JWT. If valid, it attaches the Auth{{ target: '_blank' }} object to the request object under the auth key.
You can register the plugin for all routes or limit it to specific ones.
clerkPlugin() for all routesimport 'dotenv/config' // dotenv must be imported before @clerk/fastify
import Fastify from 'fastify'
import { clerkPlugin } from '@clerk/fastify'
const fastify = Fastify({ logger: true })
fastify.register(clerkPlugin)
const start = async () => {
try {
await fastify.listen({ port: 8080 })
} catch (error) {
fastify.log.error(error)
process.exit(1)
}
}
start()
clerkPlugin() for specific routesTo apply Clerk authentication only to specific routes, register the plugin in the scope of those routes.
In the following example, the application is split into protected and public routes:
import 'dotenv/config' // dotenv must be imported before @clerk/fastify
import Fastify, { FastifyPluginCallback } from 'fastify'
import { clerkClient, clerkPlugin, getAuth } from '@clerk/fastify'
const fastify = Fastify({ logger: true })
const protectedRoutes: FastifyPluginCallback = (instance, options, done) => {
instance.register(clerkPlugin)
instance.get('/protected', async (req, reply) => {
// Use `getAuth()` to access `isAuthenticated` and the user's ID
const { isAuthenticated, userId } = getAuth(req)
// Protect the route from unauthenticated users
if (!isAuthenticated) {
return reply.code(403).send({ message: 'Access denied. Authentication required.' })
}
// Use `clerkClient` to access Clerk's JS Backend SDK methods
const user = await clerkClient.users.getUser(userId)
// Only authenticated users will see the following message
reply.send({ message: 'This is a protected route.', user })
})
done()
}
const publicRoutes: FastifyPluginCallback = (instance, options, done) => {
instance.get('/', async (req, reply) => {
reply.send({ message: 'This is a public route.' })
})
done()
}
fastify.register(protectedRoutes)
fastify.register(publicRoutes)
const start = async () => {
try {
await fastify.listen({ port: 8080 })
} catch (error) {
fastify.log.error(error)
process.exit(1)
}
}
start()
clerkPlugin() optionsThe clerkPlugin() function accepts the following options:
Determines which of Fastify's Request/Reply hooks Clerk should run. Default: 'preHandler'
</Properties>
hookName in clerkPlugin() optionsfastify.register(clerkPlugin, {
hookName: 'preHandler',
})
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā