NestJS Better Auth Fastify Module

0.0.24 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up a basic NestJS application using Fastify as the platform adapter and integrating `nestjs-better-auth-fastify` with a minimal configuration.

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();

view raw JSON →