diff --git a/docs/components/icons.tsx b/docs/components/icons.tsx index bd30421f..0d4fabc4 100644 --- a/docs/components/icons.tsx +++ b/docs/components/icons.tsx @@ -155,6 +155,27 @@ export const Icons = { ), + nestJS: (props?: SVGProps) => ( + + + + + + + + + + ), javascript: () => ( +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). + + +## Installation + +Install the NestJS integration library: + +```package-install +@thallesp/nestjs-better-auth +``` + +## Basic Setup + + +Currently, Better Auth's NestJS integration **only supports Express** and does not work with Fastify. + + +### 1. Disable Body Parser + +Disable NestJS's built-in body parser to allow Better Auth to handle the raw request body: + +```ts title="main.ts" +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(); +``` + +### 2. Import AuthModule + +Import the `AuthModule` in your root module: + +```ts title="app.module.ts" +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 {} +``` + +### 3. Protect Routes + +Use the `AuthGuard` to protect your routes: + +```ts title="user.controller.ts" +import { Controller, Get, UseGuards } from '@nestjs/common'; +import { AuthGuard, Session, UserSession } from '@thallesp/nestjs-better-auth'; + +@Controller('users') +@UseGuards(AuthGuard) +export class UserController { + @Get('me') + async getProfile(@Session() session: UserSession) { + return { user: session.user }; + } +} +``` + +## Full Documentation + +For comprehensive documentation including decorators, hooks, global guards, and advanced configuration, visit the [NestJS Better Auth repository](https://github.com/thallesp/nestjs-better-auth).