What am I doing wrong? Cookies aren't working properly

Hono (Backend Code)
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
session: {
storeSessionInDatabase: true,
},
trustedOrigins: [process.env.CORS_ORIGIN as string, "http://localhost:3001"],
advanced: {
crossSubDomainCookies:{
enabled: true,
},
defaultCookieAttributes: {
secure: true,
httpOnly: true,
sameSite: "none", // Allows CORS-based cookie sharing across subdomains
partitioned: true, // New browser standards will mandate this for foreign cookies
},
},
emailAndPassword: {
enabled: true,
requireEmailVerification: false
},
plugins: [
organization()
]
});
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
session: {
storeSessionInDatabase: true,
},
trustedOrigins: [process.env.CORS_ORIGIN as string, "http://localhost:3001"],
advanced: {
crossSubDomainCookies:{
enabled: true,
},
defaultCookieAttributes: {
secure: true,
httpOnly: true,
sameSite: "none", // Allows CORS-based cookie sharing across subdomains
partitioned: true, // New browser standards will mandate this for foreign cookies
},
},
emailAndPassword: {
enabled: true,
requireEmailVerification: false
},
plugins: [
organization()
]
});
app.use(
"/*",
cors({
origin: [process.env.CORS_ORIGIN as string, "http://localhost:8288", "http://localhost:3001"],
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["Content-Type", "Authorization"],
exposeHeaders: ["Content-Length"],
maxAge: 600,
credentials: true,
}),
);

app.on(["POST", "GET"], "/api/auth/**", (c) => auth.handler(c.req.raw));

app.use("*", async (c, next) => {
console.log("session", c.req.raw.headers)
const session = await auth.api.getSession({ headers: c.req.raw.headers });

if (!session) {
c.set("user", null);
c.set("session", null);
return next();
}

c.set("user", session.user);
c.set("session", session.session);
return next();
});
app.use(
"/*",
cors({
origin: [process.env.CORS_ORIGIN as string, "http://localhost:8288", "http://localhost:3001"],
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["Content-Type", "Authorization"],
exposeHeaders: ["Content-Length"],
maxAge: 600,
credentials: true,
}),
);

app.on(["POST", "GET"], "/api/auth/**", (c) => auth.handler(c.req.raw));

app.use("*", async (c, next) => {
console.log("session", c.req.raw.headers)
const session = await auth.api.getSession({ headers: c.req.raw.headers });

if (!session) {
c.set("user", null);
c.set("session", null);
return next();
}

c.set("user", session.user);
c.set("session", session.session);
return next();
});
NextJS Client:
onSubmit: async ({ value }) => {
await authClient.signIn.email(
{
email: value.email,
password: value.password,
},
{
onSuccess: () => {
},
onError: () => {
},
},
);
}
onSubmit: async ({ value }) => {
await authClient.signIn.email(
{
email: value.email,
password: value.password,
},
{
onSuccess: () => {
},
onError: () => {
},
},
);
}
when I use the
useSession
useSession
hook is null and in the headers have cookie after signin
3 Replies
McLovin
McLovinOP5mo ago
I sign in and sign up with success but when I am in the dashboard and try to do useSession with the auth-client it returns nothing and well, I checked the browser and there are no cookies
McLovin
McLovin3mo ago
? Would you believe that I'm having the same issue
stormej
stormej3mo ago
I have this same problem use session returns null on prod so I am unable to sign in

Did you find this page helpful?