mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
docs: add community maintained nestjs library (#3391)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1125,6 +1125,11 @@ C0.7,239.6,62.1,0.5,62.2,0.4c0,0,54,13.8,119.9,30.8S302.1,62,302.2,62c0.2,0,0.2,
|
||||
icon: Icons.nitro,
|
||||
href: "/docs/integrations/nitro",
|
||||
},
|
||||
{
|
||||
title: "NestJS",
|
||||
icon: Icons.nestJS,
|
||||
href: "/docs/integrations/nestjs",
|
||||
},
|
||||
{
|
||||
group: true,
|
||||
title: "Mobile & Desktop",
|
||||
|
||||
82
docs/content/docs/integrations/nestjs.mdx
Normal file
82
docs/content/docs/integrations/nestjs.mdx
Normal file
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: NestJS Integration
|
||||
description: Integrate Better Auth with NestJS.
|
||||
---
|
||||
|
||||
This guide will show you how to integrate Better Auth with [NestJS](https://nestjs.com/).
|
||||
|
||||
Before you start, make sure you have a Better Auth instance configured. If you haven't done that yet, check out the [installation](/docs/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>
|
||||
|
||||
## Installation
|
||||
|
||||
Install the NestJS integration library:
|
||||
|
||||
```package-install
|
||||
@thallesp/nestjs-better-auth
|
||||
```
|
||||
|
||||
## Basic Setup
|
||||
|
||||
<Callout type="warn">
|
||||
Currently, Better Auth's NestJS integration **only supports Express** and does not work with Fastify.
|
||||
</Callout>
|
||||
|
||||
### 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).
|
||||
Reference in New Issue
Block a user