#Better Auth Implementation in NestJS
11 messages · Page 1 of 1 (latest)
hey, did you got it?
iam trying to do the same
I've gotten as far as this:
import { All, Controller, Req, Res } from "@nestjs/common";
import { toNodeHandler } from "better-auth/node";
import { auth } from "@/lib/auth";
import type { Request, Response } from "express";
@Controller("auth")
export class AuthController {
private readonly handler = toNodeHandler(auth.handler);
@All("*path")
async handleAuth(@Req() req: Request, @Res() res: Response) {
await this.handler(req, res);
}
}
Which works - almost! It's set up (apparently) successfully with Drizzle and Better Auth. The issue I'm currently running into though, is that for POST requests with a body, I get the following error:
[Nest] 15512 - 19/02/2025, 19:47:37 ERROR [ExceptionsHandler] TypeError: Response body object should not be disturbed or locked
at extractBody (node:internal/deps/undici/undici:5518:17)
at new Request (node:internal/deps/undici/undici:9697:48)
at getRequest (C:\Users\Matt\Documents\Code\Nest-BetterAuth-Drizzle\node_modules\better-call\src\adapter\request.ts:98:9)
at AuthController.handler (C:\Users\Matt\Documents\Code\Nest-BetterAuth-Drizzle\node_modules\better-call\src\adapter\node.ts:11:34)
at AuthController.handleAuth (C:\Users\Matt\Documents\Code\Nest-BetterAuth-Drizzle\src\modules\auth\auth.controller.ts:12:16)
at C:\Users\Matt\Documents\Code\Nest-BetterAuth-Drizzle\node_modules\@nestjs\core\router\router-execution-context.js:38:29
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async C:\Users\Matt\Documents\Code\Nest-BetterAuth-Drizzle\node_modules\@nestjs\core\router\router-execution-context.js:46:28
at async C:\Users\Matt\Documents\Code\Nest-BetterAuth-Drizzle\node_modules\@nestjs\core\router\router-proxy.js:9:17
If anyone has any info on how I could fix this, I'd be very happy, as I simply can't seem to figure out how to troubleshoot this properly. My hypothesis is that it's getting into errors due to the way Express is internally set up in NestJS - seeing the Better Auth docs requires to register express.json() after registering the Auth endpoint. Not really sure what to do about it though.
I did find a working way, registering the auth endpoint in the main.ts file, although I wish it would be possible in its own module for further expansion and cleaner separation with the rest of the code - maybe someone sees this in the future and can give me advice... Anyway, here's how you can register it in the main.ts file:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { toNodeHandler } from "better-auth/node";
import { auth } from "@/lib/auth";
async function bootstrap() {
const app = (await NestFactory.create(AppModule)).setGlobalPrefix("api");
app.getHttpAdapter().all("/api/auth/*path", toNodeHandler(auth));
await app.listen(process.env.PORT ?? 3000);
}
void bootstrap();
ayoo, thanks 🙏
hey! im doing a PR on nestjs support and would love some feedback and testing
https://github.com/better-auth/better-auth/pull/1548
@waxen heron Did you get this working or did you fallback to the package from Thalles?
I think I remained with this for my project: #1321456103455854672 message
Created some guards/decorators or so to go with it
Felt a bit weird, but it worked