Better-auth not working Vercel deployed project but works well in development?

export const auth = betterAuth({ basePath: "/api/auth", baseURL: process.env.BETTER_AUTH_URL, database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true, requireEmailVerification: true, }, // some code databaseHooks: { user: { create: { async before(user) { // modify user before it is created for adding our custom id const { data: id } = await generateCustomId({ entityTybe: "users" }); return { data: { ...user, id: id, }, }; }, }, }, }, advanced: { database: { generateId: false, }, }, session: { expiresIn: 60 * 60 * 24 * 30, }, user: { additionalFields: { banned: { type: "boolean", input: false, }, role: { type: ["admin", "user", "manager"], input: true, }, banReason: { type: "string", input: false, }, banExpires: { type: "date", input: false, }, }, }, plugins: [ nextCookies(), admin({ ac: accessControl, roles: { user: userRole, admin: adminRole, manager: managerRole, }, adminRoles: ["admin", "manager"], defaultRole: "user", bannedUserMessage: "You have been banned to login! please contact Deero Admin Team for support.!!", }), ], }); export type ErrorCodes = keyof typeof auth.$ERROR_CODES | "unknown"; and I am using authClient.useSessson to get session of the user.
2 Replies
walhallyus
walhallyus2w ago
I asked My Better Auth AI Agent about this — here’s the full response: 🔗 Direct chat link to the answer You can also start your own chat with this agent to explore Better Auth documentation and examples here: 🌐 https://app.mojar.ai/agent/f5210e50-5f2e-4dc4-84f1-d9666afc859f/chat
Mojar
Intelligent knowledge discovery reimagined
Mojar
Intelligent knowledge discovery reimagined
walhallyus
walhallyus2w ago
i think this is the key For server-side session checks (e.g., in a Next.js Server Component or middleware), you should use auth.api.getSession and pass the request headers.
// app/dashboard/page.tsx
import { auth } from "@/lib/auth"; // Assuming your auth instance is exported from here
import { headers } from "next/headers";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
const session = await auth.api.getSession({ headers: await headers() });

if (!session) {
redirect("/sign-in");
}

return (
<div>
<h1>Welcome {session.user.name}</h1>
</div>
);
}
// app/dashboard/page.tsx
import { auth } from "@/lib/auth"; // Assuming your auth instance is exported from here
import { headers } from "next/headers";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
const session = await auth.api.getSession({ headers: await headers() });

if (!session) {
redirect("/sign-in");
}

return (
<div>
<h1>Welcome {session.user.name}</h1>
</div>
);
}

Did you find this page helpful?