[PR #1548] [CLOSED] feat: nestjs support #12108

Closed
opened 2026-04-13 08:10:52 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/1548
Author: @ThallesP
Created: 2/23/2025
Status: Closed

Base: mainHead: feat/add-support-for-nestjs


📝 Commits (10+)

  • 7d64be0 wip: write docs on how I want this to look like (still not there)
  • 7988310 feat: add the basic integration
  • 4bfafaa feat: add guard and decorators
  • fcb3376 feat: add nest example
  • e0e2522 experimental: support for fastify
  • 150bec4 feat: add auth service
  • fe15d57 Merge branch 'main' of github.com:better-auth/better-auth into feat/add-support-for-nestjs
  • 8ca2fe8 Merge branch 'main' of github.com:better-auth/better-auth into feat/add-support-for-nestjs
  • eae0a5c wip: still studying on how i'll create this decorator
  • 6b201a4 Merge branch 'main' of github.com:better-auth/better-auth into feat/add-support-for-nestjs

📊 Changes

33 files changed (+15746 additions, -7083 deletions)

View changed files

📝 biome.json (+6 -1)
📝 docs/components/icons.tsx (+21 -0)
📝 docs/components/sidebar-content.tsx (+5 -0)
📝 docs/content/docs/installation.mdx (+43 -1)
docs/content/docs/integrations/nestjs.mdx (+304 -0)
📝 docs/package.json (+3 -0)
examples/nestjs-example/.gitignore (+56 -0)
examples/nestjs-example/README.md (+99 -0)
examples/nestjs-example/nest-cli.json (+8 -0)
examples/nestjs-example/package.json (+66 -0)
examples/nestjs-example/src/app.controller.ts (+34 -0)
examples/nestjs-example/src/app.module.ts (+11 -0)
examples/nestjs-example/src/auth.ts (+8 -0)
examples/nestjs-example/src/hooks/before-signup.hook.ts (+10 -0)
examples/nestjs-example/src/hooks/hooks.module.ts (+8 -0)
examples/nestjs-example/src/main.ts (+11 -0)
examples/nestjs-example/test/jest-e2e.json (+9 -0)
examples/nestjs-example/tsconfig.build.json (+4 -0)
examples/nestjs-example/tsconfig.json (+21 -0)
package-lock.json (+4138 -0)

...and 13 more files

📄 Description

EDIT: moved to https://github.com/thallesp/nestjs-better-auth

Adds support for the NestJS framework

The PR adds full support for NestJS with decorators, guards, and a module for a seamless integration.

Here's some examples:

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    bodyParser: false, // Disable NestJS body parser to let Better Auth handle raw requests
  });
  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
import { Module } from '@nestjs/common';
import { AuthModule } from 'better-auth/nestjs';
import { auth } from "./auth"; // path to your auth file

@Module({
  imports: [
    AuthModule.forRoot(auth),
  ],
})
export class AppModule {}

Protect routes using AuthGuard:

import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard, Session } from 'better-auth/nestjs';

@Controller('users')
@UseGuards(AuthGuard) // Protect all routes in this controller
export class UsersController {
  @Get('me')
  getProfile(@Session() session) {
    return session;
  }
}

Enough talking, give me the code!

Fine! You can install the NestJS better-auth by using the following command:

pnpm i https://pkg.pr.new/better-auth/better-auth@1548

And for docs:
https://github.com/ThallesP/better-auth/blob/feat/add-support-for-nestjs/docs/content/docs/integrations/nestjs.mdx

fixes #171


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/1548 **Author:** [@ThallesP](https://github.com/ThallesP) **Created:** 2/23/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/add-support-for-nestjs` --- ### 📝 Commits (10+) - [`7d64be0`](https://github.com/better-auth/better-auth/commit/7d64be09dca8302052702d44777a9c74cc838c8e) wip: write docs on how I want this to look like (still not there) - [`7988310`](https://github.com/better-auth/better-auth/commit/7988310450326a1ebe52853d1e324ddb4484d188) feat: add the basic integration - [`4bfafaa`](https://github.com/better-auth/better-auth/commit/4bfafaaecbf070bc7039ab6cfdd3976bf2f56775) feat: add guard and decorators - [`fcb3376`](https://github.com/better-auth/better-auth/commit/fcb3376f02ab293becc69dbd3d15c202d1af1b4b) feat: add nest example - [`e0e2522`](https://github.com/better-auth/better-auth/commit/e0e2522feb87ec1122937cb59ed070ac152b71da) experimental: support for fastify - [`150bec4`](https://github.com/better-auth/better-auth/commit/150bec4f31ad7e61937cf6949e02bdbbd1eab5a7) feat: add auth service - [`fe15d57`](https://github.com/better-auth/better-auth/commit/fe15d5722bd13c15f710dcdfc271cdd87fd2ff2c) Merge branch 'main' of github.com:better-auth/better-auth into feat/add-support-for-nestjs - [`8ca2fe8`](https://github.com/better-auth/better-auth/commit/8ca2fe81cea8087678b769b55eb950863601d189) Merge branch 'main' of github.com:better-auth/better-auth into feat/add-support-for-nestjs - [`eae0a5c`](https://github.com/better-auth/better-auth/commit/eae0a5c02badaf977b91bc2a108542aa8985aa81) wip: still studying on how i'll create this decorator - [`6b201a4`](https://github.com/better-auth/better-auth/commit/6b201a4ed09b181b94ad6823d334eca30a52aee5) Merge branch 'main' of github.com:better-auth/better-auth into feat/add-support-for-nestjs ### 📊 Changes **33 files changed** (+15746 additions, -7083 deletions) <details> <summary>View changed files</summary> 📝 `biome.json` (+6 -1) 📝 `docs/components/icons.tsx` (+21 -0) 📝 `docs/components/sidebar-content.tsx` (+5 -0) 📝 `docs/content/docs/installation.mdx` (+43 -1) ➕ `docs/content/docs/integrations/nestjs.mdx` (+304 -0) 📝 `docs/package.json` (+3 -0) ➕ `examples/nestjs-example/.gitignore` (+56 -0) ➕ `examples/nestjs-example/README.md` (+99 -0) ➕ `examples/nestjs-example/nest-cli.json` (+8 -0) ➕ `examples/nestjs-example/package.json` (+66 -0) ➕ `examples/nestjs-example/src/app.controller.ts` (+34 -0) ➕ `examples/nestjs-example/src/app.module.ts` (+11 -0) ➕ `examples/nestjs-example/src/auth.ts` (+8 -0) ➕ `examples/nestjs-example/src/hooks/before-signup.hook.ts` (+10 -0) ➕ `examples/nestjs-example/src/hooks/hooks.module.ts` (+8 -0) ➕ `examples/nestjs-example/src/main.ts` (+11 -0) ➕ `examples/nestjs-example/test/jest-e2e.json` (+9 -0) ➕ `examples/nestjs-example/tsconfig.build.json` (+4 -0) ➕ `examples/nestjs-example/tsconfig.json` (+21 -0) ➕ `package-lock.json` (+4138 -0) _...and 13 more files_ </details> ### 📄 Description EDIT: moved to https://github.com/thallesp/nestjs-better-auth # Adds support for the NestJS framework The PR adds full support for NestJS with decorators, guards, and a module for a seamless integration. Here's some examples: ```ts import { NestFactory } from "@nestjs/core"; import { AppModule } from "./app.module"; async function bootstrap() { const app = await NestFactory.create(AppModule, { bodyParser: false, // Disable NestJS body parser to let Better Auth handle raw requests }); await app.listen(process.env.PORT ?? 3000); } bootstrap(); ``` ```ts import { Module } from '@nestjs/common'; import { AuthModule } from 'better-auth/nestjs'; import { auth } from "./auth"; // path to your auth file @Module({ imports: [ AuthModule.forRoot(auth), ], }) export class AppModule {} ``` Protect routes using AuthGuard: ```ts import { Controller, Get, UseGuards } from '@nestjs/common'; import { AuthGuard, Session } from 'better-auth/nestjs'; @Controller('users') @UseGuards(AuthGuard) // Protect all routes in this controller export class UsersController { @Get('me') getProfile(@Session() session) { return session; } } ``` # Enough talking, give me the code! Fine! You can install the NestJS better-auth by using the following command: ``` pnpm i https://pkg.pr.new/better-auth/better-auth@1548 ``` And for docs: https://github.com/ThallesP/better-auth/blob/feat/add-support-for-nestjs/docs/content/docs/integrations/nestjs.mdx fixes #171 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 08:10:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#12108