[GH-ISSUE #2027] BetterAuth with NestJS #17644

Closed
opened 2026-04-15 15:50:32 -05:00 by GiteaMirror · 9 comments
Owner

Originally created by @sovkay on GitHub (Mar 27, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2027

Hey people,

I was able to get NestJS, and React Router v7 to work with better-auth without too many issues but during my research I found people mentioned lots of issues while setting up with NestJS.

Can someone please take a look, and let me if this is ok security and feature set wise?

And What might be the edge-cases, To me all seems to be working fine as of now.

async function bootstrap() {
	// Create express instance
	const server = express();

	// Logging middleware
	server.use(morgan("dev"));

	// Apply CORS middleware first
	server.use(
		cors({
			origin: process.env.CORS_ORIGIN || true,
			credentials: true,
			methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
			allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
		}),
	);

	// Auth routes after middleware
	server.all("/api/auth/*", toNodeHandler(auth));

	server.use(express.urlencoded({ extended: true }));

	// Create NestJS app with explicit bodyParser: false
	const app = await NestFactory.create<NestExpressApplication>(
		AppModule,
		new ExpressAdapter(server),
	);

	await app.listen(process.env.PORT ?? 4000);
}

bootstrap();
Originally created by @sovkay on GitHub (Mar 27, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2027 Hey people, I was able to get NestJS, and React Router v7 to work with better-auth without too many issues but during my research I found people mentioned lots of issues while setting up with NestJS. Can someone please take a look, and let me if this is ok security and feature set wise? And What might be the edge-cases, To me all seems to be working fine as of now. ```ts async function bootstrap() { // Create express instance const server = express(); // Logging middleware server.use(morgan("dev")); // Apply CORS middleware first server.use( cors({ origin: process.env.CORS_ORIGIN || true, credentials: true, methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"], }), ); // Auth routes after middleware server.all("/api/auth/*", toNodeHandler(auth)); server.use(express.urlencoded({ extended: true })); // Create NestJS app with explicit bodyParser: false const app = await NestFactory.create<NestExpressApplication>( AppModule, new ExpressAdapter(server), ); await app.listen(process.env.PORT ?? 4000); } bootstrap(); ```
GiteaMirror added the locked label 2026-04-15 15:50:32 -05:00
Author
Owner

@yeferson59 commented on GitHub (Mar 28, 2025):

Hi @ksorv, there is a PR about support to nestjs you can watch it https://github.com/better-auth/better-auth/pull/1548

<!-- gh-comment-id:2761310891 --> @yeferson59 commented on GitHub (Mar 28, 2025): Hi @ksorv, there is a PR about support to nestjs you can watch it https://github.com/better-auth/better-auth/pull/1548
Author
Owner

@sovkay commented on GitHub (Mar 29, 2025):

Thanks @yeferson59, I thought it was not stable enough? Let me try to use this.

<!-- gh-comment-id:2763074474 --> @sovkay commented on GitHub (Mar 29, 2025): Thanks @yeferson59, I thought it was not stable enough? Let me try to use this.
Author
Owner

@yeferson59 commented on GitHub (Mar 29, 2025):

@ksorv I've been testing it a bit but haven't reviewed it completely but it looks good how it's going. Try it!

<!-- gh-comment-id:2763113176 --> @yeferson59 commented on GitHub (Mar 29, 2025): @ksorv I've been testing it a bit but haven't reviewed it completely but it looks good how it's going. Try it!
Author
Owner

@brookmg commented on GitHub (Mar 30, 2025):

@ksorv, can you share which express version you're using? I was facing TypeError: Missing parameter name at 11: https://git.new/pathToRegexpError when directly setting up better-auth with express.

<!-- gh-comment-id:2764633485 --> @brookmg commented on GitHub (Mar 30, 2025): @ksorv, can you share which express version you're using? I was facing `TypeError: Missing parameter name at 11: https://git.new/pathToRegexpError` when directly setting up better-auth with express.
Author
Owner

@sovkay commented on GitHub (Apr 7, 2025):

Hey, I was using latest express that came with NestJs i think v5.

@yeferson59 really good bro, This things works really well. Well thought out too.

<!-- gh-comment-id:2781952370 --> @sovkay commented on GitHub (Apr 7, 2025): Hey, I was using latest express that came with NestJs i think v5. @yeferson59 really good bro, This things works really well. Well thought out too.
Author
Owner

@krabhi1 commented on GitHub (Apr 21, 2025):

so it's a correct way?

<!-- gh-comment-id:2818401556 --> @krabhi1 commented on GitHub (Apr 21, 2025): so it's a correct way?
Author
Owner

@andreghisleni commented on GitHub (Jun 17, 2025):

Hey, Can you share yuor repository with this example?? with nestjs and react router 7??

<!-- gh-comment-id:2980536023 --> @andreghisleni commented on GitHub (Jun 17, 2025): Hey, Can you share yuor repository with this example?? with nestjs and react router 7??
Author
Owner

@yeferson59 commented on GitHub (Jun 17, 2025):

Hey, there is already an external library to integrate nestjs with betterAuth. Here is the repository https://github.com/ThallesP/nestjs-better-auth, this is integration with the previous PR

<!-- gh-comment-id:2980642334 --> @yeferson59 commented on GitHub (Jun 17, 2025): Hey, there is already an external library to integrate nestjs with betterAuth. Here is the repository https://github.com/ThallesP/nestjs-better-auth, this is integration with the previous PR
Author
Owner

@dosubot[bot] commented on GitHub (Sep 16, 2025):

Hi, @sovkay. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary

  • You shared your integration of BetterAuth with NestJS and React Router v7, seeking feedback on security and completeness.
  • Community members referenced PR #1548 and the nestjs-better-auth library as relevant resources.
  • You confirmed using the latest Express version bundled with NestJS after a related error was mentioned.
  • Several users expressed interest in example repositories to replicate your setup.
  • The issue remains unresolved with no recent updates.

Next Steps

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I don’t hear back within 7 days, this issue will be automatically closed.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3299427602 --> @dosubot[bot] commented on GitHub (Sep 16, 2025): Hi, @sovkay. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary** - You shared your integration of BetterAuth with NestJS and React Router v7, seeking feedback on security and completeness. - Community members referenced PR #1548 and the nestjs-better-auth library as relevant resources. - You confirmed using the latest Express version bundled with NestJS after a related error was mentioned. - Several users expressed interest in example repositories to replicate your setup. - The issue remains unresolved with no recent updates. **Next Steps** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - If I don’t hear back within 7 days, this issue will be automatically closed. Thanks for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#17644