docs: update nestjs section (#5029)

This commit is contained in:
Thalles Passos
2025-10-01 21:26:56 -03:00
committed by Alex Yang
parent 6bb945efed
commit 8b13d387bd

View File

@@ -22,7 +22,7 @@ Install the NestJS integration library:
## Basic Setup ## Basic Setup
<Callout type="warn"> <Callout type="warn">
Currently, Better Auth's NestJS integration **only supports Express** and does not work with Fastify. Currently the library has beta support for Fastify, if you experience any issues with it, please open an issue at [nestjs-better-auth](https://github.com/ThallesP/nestjs-better-auth).
</Callout> </Callout>
### 1. Disable Body Parser ### 1. Disable Body Parser
@@ -53,27 +53,40 @@ import { auth } from "./auth"; // Your Better Auth instance
@Module({ @Module({
imports: [ imports: [
AuthModule.forRoot(auth), AuthModule.forRoot({ auth }),
], ],
}) })
export class AppModule {} export class AppModule {}
``` ```
### 3. Protect Routes ### 3. Route Protection
Use the `AuthGuard` to protect your routes: **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:
```ts title="user.controller.ts" ```ts title="user.controller.ts"
import { Controller, Get, UseGuards } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
import { AuthGuard, Session, UserSession } from '@thallesp/nestjs-better-auth'; import { Session, UserSession, AllowAnonymous, OptionalAuth } from '@thallesp/nestjs-better-auth';
@Controller('users') @Controller('users')
@UseGuards(AuthGuard)
export class UserController { export class UserController {
@Get('me') @Get('me')
async getProfile(@Session() session: UserSession) { async getProfile(@Session() session: UserSession) {
return { user: session.user }; 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 };
}
} }
``` ```