Monorepo + Hono

@Ping basically i don't want to manually write fetch for all better auth endpoints, would prefer using the auth.api method any ideas on how to go about this pls I have a setup with Hono + Better Auth on the backend and Next.js on the frontend. Backend Better Auth config (running on Hono, port 3000): This is in my backend folder /lib/auth.ts which i then exported using exports in the package.json and imported in my frontend
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../db";
import * as schema from "../db/schema/auth";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: schema,
}),
trustedOrigins: [process.env.CORS_ORIGIN || ""],
emailAndPassword: {
enabled: true,
},
advanced: {
defaultCookieAttributes: {
sameSite: "none",
secure: true,
httpOnly: true,
},
},
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../db";
import * as schema from "../db/schema/auth";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: schema,
}),
trustedOrigins: [process.env.CORS_ORIGIN || ""],
emailAndPassword: {
enabled: true,
},
advanced: {
defaultCookieAttributes: {
sameSite: "none",
secure: true,
httpOnly: true,
},
},
});
On the Next.js frontend (running on port 3001), I’m importing the auth object from the Hono backend to make server-related queries. Problem: When I run
const session = await auth.api.getSession({ headers: await headers() });
const session = await auth.api.getSession({ headers: await headers() });
inside a Next.js server component, it always returns null. But when I use authClient.useSession on a client component, the session works fine. IG the first thing to confirm is that is it possible to use the exported auth from the hono app
1 Reply
Vishal Anton
Vishal Anton6d ago
@iatomic.btc From what I heard it is better to write endpoints by yourself using auth.api I believe it is more performant

Did you find this page helpful?