Fastify Better Auth is a Fastify plugin that simplifies the integration of the Better Auth library into your Fastify applications. This plugin allows you to easily register authentication routes using Better Auth, providing a seamless authentication experience.
npm install fastify-better-auth
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import FastifyBetterAuth from 'fastify-better-auth';
import fp from 'fastify-plugin';
export const auth = betterAuth({
trustedOrigins: [env.auth.URL],
database: drizzleAdapter(db, {
provider: 'pg',
usePlural: true,
}),
emailAndPassword: {
enabled: true,
},
});
async function authPlugin(fastify) {
await fastify.register(FastifyBetterAuth, { auth });
}
export default fp(authPlugin, {
name: 'auth-plugin',
});
You can now access the auth instance from the Fastify instance. For example if you want get the current session you can do:
const session = await fastify.auth.api.getSession({
headers: fromNodeHeaders(req.headers),
});
This project is licensed under the ISC License - see the LICENSE file for details.