docs/content/docs/integrations/nestjs.mdx
This guide will show you how to integrate Better Auth with NestJS.
Before you start, make sure you have a Better Auth instance configured. If you haven't done that yet, check out the installation.
<Callout type="info"> The NestJS integration is **community maintained**. If you encounter any issues, please open them at [nestjs-better-auth](https://github.com/ThallesP/nestjs-better-auth). </Callout>Install the NestJS integration library:
@thallesp/nestjs-better-auth
Disable NestJS's built-in body parser to allow Better Auth to handle the raw request body:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
bodyParser: false, // Required for Better Auth
});
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
Import the AuthModule in your root module:
import { Module } from '@nestjs/common';
import { AuthModule } from '@thallesp/nestjs-better-auth';
import { auth } from "./auth"; // Your Better Auth instance
@Module({
imports: [
AuthModule.forRoot({ auth }),
],
})
export class AppModule {}
Global by default: An AuthGuard is registered globally by this module. All routes are protected unless you explicitly allow access.
Use the Session decorator to access the user session:
import { Controller, Get } from '@nestjs/common';
import { Session, UserSession, AllowAnonymous, OptionalAuth } from '@thallesp/nestjs-better-auth';
@Controller('users')
export class UserController {
@Get('me')
async getProfile(@Session() session: UserSession) {
return { user: session.user };
}
@Get('public')
@AllowAnonymous() // Allow anonymous access
async getPublic() {
return { message: 'Public route' };
}
@Get('optional')
@OptionalAuth() // Authentication is optional
async getOptional(@Session() session: UserSession) {
return { authenticated: !!session };
}
}
For comprehensive documentation including decorators, hooks, global guards, and advanced configuration, visit the NestJS Better Auth repository.