routes return null

I'm using better-auth with fastify and all my auth routes are returning only "null" on the body. i'm kinda lost here trying to debug the reason. (yeah tried ask llms but even them can't help much) // auth.ts
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../utils/db";
import {
user as userTable,
account as accountTable,
session as sessionTable,
verification as verificationTable,
} from "../database/schema/index";
import dotenv from "dotenv";
dotenv.config();

export const auth = betterAuth({
baseURL: process.env.BASE_URL || "http://localhost:3000/api/v1",
trustedOrigins: [process.env.CLIENT_ORIGIN || "http://localhost:3000/api/v1"],
secret: process.env.BETTER_AUTH_SECRET || "secret",
emailAndPassword: {
enabled: true,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
},
advanced: {
cookiePrefix: "chatstore_",
},
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user: userTable,
account: accountTable,
session: sessionTable,
verification: verificationTable,
},
}),
logger: {
level: process.env.NODE_ENV === "development" ? "debug" : "info",
},
customIdGenerator: true,
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../utils/db";
import {
user as userTable,
account as accountTable,
session as sessionTable,
verification as verificationTable,
} from "../database/schema/index";
import dotenv from "dotenv";
dotenv.config();

export const auth = betterAuth({
baseURL: process.env.BASE_URL || "http://localhost:3000/api/v1",
trustedOrigins: [process.env.CLIENT_ORIGIN || "http://localhost:3000/api/v1"],
secret: process.env.BETTER_AUTH_SECRET || "secret",
emailAndPassword: {
enabled: true,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
},
advanced: {
cookiePrefix: "chatstore_",
},
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user: userTable,
account: accountTable,
session: sessionTable,
verification: verificationTable,
},
}),
logger: {
level: process.env.NODE_ENV === "development" ? "debug" : "info",
},
customIdGenerator: true,
});
3 Replies
NotXForts
NotXFortsOP•2mo ago
// index.ts
import Fastify from "fastify";
import usersModule from "./modules/users";
import healthModule from "./modules/health";
import cors from "./utils/cors";
import authModule from "./modules/auth";

const app = Fastify({ logger: true });

app.register(cors);

app.register(
async function (app) {
app.register(usersModule);
app.register(healthModule);
app.register(authModule);
},
{ prefix: "/api/v1" },
);

app.get("/", async (request: any, reply: any) => {
return { hello: "world" };
});

const start = async () => {
try {
await app.listen({ port: 3000, host: "0.0.0.0" });
console.log("🚀 Server running at http://localhost:3000");
console.log(app.printRoutes());
} catch (err) {
app.log.error(err);
process.exit(1);
}
};

start();
import Fastify from "fastify";
import usersModule from "./modules/users";
import healthModule from "./modules/health";
import cors from "./utils/cors";
import authModule from "./modules/auth";

const app = Fastify({ logger: true });

app.register(cors);

app.register(
async function (app) {
app.register(usersModule);
app.register(healthModule);
app.register(authModule);
},
{ prefix: "/api/v1" },
);

app.get("/", async (request: any, reply: any) => {
return { hello: "world" };
});

const start = async () => {
try {
await app.listen({ port: 3000, host: "0.0.0.0" });
console.log("🚀 Server running at http://localhost:3000");
console.log(app.printRoutes());
} catch (err) {
app.log.error(err);
process.exit(1);
}
};

start();
NotXForts
NotXFortsOP•2mo ago
No description
NotXForts
NotXFortsOP•2mo ago
so looks like is the way i setup my routes. i placed it in a separated file, then i customized the auth url but it causes the endpoint to stop working for some reason. i set /api/v1/auth/* (it does not work) but /api/auth/* works for some reason it does not let me customize the url. that sucks i wanted to add a api version

Did you find this page helpful?