NestJS Better Auth Fastify Module
The `nestjs-better-auth-fastify` package provides a dedicated NestJS module for integrating authentication capabilities specifically when using the Fastify platform adapter. It acts as a bridge, leveraging the underlying `better-auth` library to deliver authentication services tailored for high-performance Fastify-based NestJS applications. Currently, the package is in early development, reflected by its 0.0.24 version, which implies that API surfaces may change without adhering to strict semantic versioning, although recent releases show largely minor updates. Its core differentiator lies in its optimized integration with Fastify, providing a specialized solution for developers prioritizing Fastify's performance benefits within the NestJS ecosystem, rather than a generic authentication module. While recent changes have been minimal, early versions typically address fundamental architectural considerations and bug fixes, such as making `@nestjs/graphql` an optional dependency.
Common errors
-
Error: Nest can't resolve dependencies of the BetterAuthFastifyModule (?). Please make sure that the argument at index [0] is available in the RootTestModule context.
cause The `better-auth` library (or its dependencies) is not properly installed or accessible, or the configuration passed to `forRoot` is malformed.fixVerify `better-auth` is listed in your `package.json` and installed. Check the `forRoot` configuration for any missing required properties or incorrect types as per `better-auth`'s documentation. -
TypeError: app.listen is not a function
cause Attempting to use Fastify-specific application methods (like `listen` on `NestFastifyApplication`) on a generic `INestApplication` instance, likely because the `FastifyAdapter` was not correctly passed during `NestFactory.create`.fixEnsure `NestFactory.create` is explicitly typed and instantiated with `new FastifyAdapter()`. Example: `await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());`
Warnings
- breaking As a package in the 0.0.x version range, any release may introduce breaking changes without adhering to strict semantic versioning (e.g., a minor release might contain breaking API changes). Always review the changelog before upgrading.
- gotcha Prior to v0.0.17, the `@nestjs/graphql` peer dependency was not truly optional, potentially causing installation or runtime issues if GraphQL was not used. This has since been fixed using dynamic imports.
- gotcha The module requires `@nestjs/platform-fastify` and `fastify` to be correctly installed and configured in your NestJS application. Using a different platform adapter (e.g., Express) will lead to runtime errors or incorrect behavior.
Install
-
npm install nestjs-better-auth-fastify -
yarn add nestjs-better-auth-fastify -
pnpm add nestjs-better-auth-fastify
Imports
- BetterAuthFastifyModule
const BetterAuthFastifyModule = require('nestjs-better-auth-fastify').BetterAuthFastifyModule;import { BetterAuthFastifyModule } from 'nestjs-better-auth-fastify'; - BetterAuthGuard
import { BetterAuthGuard } from 'nestjs-better-auth-fastify'; - BetterAuthConfig
import { BetterAuthConfig } from 'nestjs-better-auth-fastify';
Quickstart
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { Module } from '@nestjs/common';
import { BetterAuthFastifyModule } from 'nestjs-better-auth-fastify';
@Module({
imports: [
BetterAuthFastifyModule.forRoot({
secret: process.env.AUTH_SECRET ?? 'super-secret-jwt-key',
expiresIn: '1h',
// Other better-auth specific configurations would go here
// For example, strategy configuration:
// strategies: [
// { name: 'jwt', handler: () => ({ /* JWT strategy setup */ }) }
// ]
}),
],
controllers: [],
providers: [],
})
class AppModule {}
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
app.listen(3000, () => {
console.log('NestJS application with Fastify and BetterAuth listening on port 3000');
});
}
bootstrap();