NestJs + Better Auth

I followed all the step-by-step instructions from Better Auth for NestJs, but even though the console shows [Nest] 36 - 09/08/2025, 1:02:55 PM LOG [AuthModule] AuthModule initialized BetterAuth on '/api/auth/*, when I try to make a request to /api/auth/ok, I get a message saying the route was not found.
No description
5 Replies
Diego Crivelaro
Diego CrivelaroOP2mo ago
// app.module.ts
import { ValidationPipe } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";

import helmet from "helmet";

import { AppModule } from "@modules/app/app.module";

async function bootstrap() {
const SERVER_PORT = process.env.SERVER_PORT || 3333;
const app = await NestFactory.create(AppModule, {
bodyParser: false,
});

app.use(helmet());
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
always: true,
}),
);

console.log(`Server is running on port ${SERVER_PORT}`);

await app.listen(SERVER_PORT);
}

bootstrap();
// app.module.ts
import { ValidationPipe } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";

import helmet from "helmet";

import { AppModule } from "@modules/app/app.module";

async function bootstrap() {
const SERVER_PORT = process.env.SERVER_PORT || 3333;
const app = await NestFactory.create(AppModule, {
bodyParser: false,
});

app.use(helmet());
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
always: true,
}),
);

console.log(`Server is running on port ${SERVER_PORT}`);

await app.listen(SERVER_PORT);
}

bootstrap();
// src/lib/auth.ts
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";

import { PrismaClient } from "@@prisma/client";

const prisma = new PrismaClient();

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: `postgresql`,
}),
baseURL: `http://localhost:3333`,
emailAndPassword: {
enabled: true,
},
}) as unknown;
// src/lib/auth.ts
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";

import { PrismaClient } from "@@prisma/client";

const prisma = new PrismaClient();

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: `postgresql`,
}),
baseURL: `http://localhost:3333`,
emailAndPassword: {
enabled: true,
},
}) as unknown;
//app.module.ts
import { Module } from "@nestjs/common";

import { auth } from "src/lib/auth";

import { CommonModule } from "@common/common.module";
import { DatabaseModule } from "@database/database.module";
import { PrismaService } from "@database/prisma.service";
import { AppController } from "@modules/app/app.controller";
import { AppService } from "@modules/app/app.service";
import { UsersModule } from "@modules/users/users.module";
import { AuthModule } from "@thallesp/nestjs-better-auth";

@Module({
imports: [
CommonModule,
DatabaseModule,
AuthModule.forRoot(auth),
UsersModule,
],
controllers: [AppController],
providers: [AppService, PrismaService],
})
export class AppModule {}
//app.module.ts
import { Module } from "@nestjs/common";

import { auth } from "src/lib/auth";

import { CommonModule } from "@common/common.module";
import { DatabaseModule } from "@database/database.module";
import { PrismaService } from "@database/prisma.service";
import { AppController } from "@modules/app/app.controller";
import { AppService } from "@modules/app/app.service";
import { UsersModule } from "@modules/users/users.module";
import { AuthModule } from "@thallesp/nestjs-better-auth";

@Module({
imports: [
CommonModule,
DatabaseModule,
AuthModule.forRoot(auth),
UsersModule,
],
controllers: [AppController],
providers: [AppService, PrismaService],
})
export class AppModule {}
"better-auth": "1.3.8",
"@thallesp/nestjs-better-auth": "1.0.3",
"better-auth": "1.3.8",
"@thallesp/nestjs-better-auth": "1.0.3",
Diego Crivelaro
Diego CrivelaroOP2mo ago
I found this PR about the same issue I’m facing, but in theory it should have already been resolved. https://github.com/ThallesP/nestjs-better-auth/issues/20
GitHub
/api/auth/* always return 404 · Issue #20 · ThallesP/nestjs-bet...
I am encountering an issue where all requests to endpoints under the /api/auth/* path result in a 404 Not Found error.This occurs even with a minimal project setup using better-auth and nestjs-bett...
electrode
electrode4w ago
try changing your baseURL to include the auth path: http://localhost:3333/api/auth
KiNFiSH
KiNFiSH4w ago
if there is path mentioned on baseURL , basePath will be ignored
Diego Crivelaro
Diego CrivelaroOP3w ago
I managed to solve the problem! Thank you very much!

Did you find this page helpful?