user session null

Hi all, I searched and saw a few posts related to my problem but the solution was not enough so I decided to make my own post. I am trying out Better-Auth with Prisma. Problem: I don't see user session after signing up or logging in. I am able to see the db populate with the information + session data. What I'm trying to do: 1. sign up / login 2. redirect to a different page, i.e / -> /setup 3. do stuff. Here is my current code for the configuration:
import { betterAuth } from 'better-auth';
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from '../../prismdb/prismdb';

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
trustedOrigins: [`localhost`],
advanced: {
database: { useNumberId: true },
crossSubDomainCookies: {
enabled: true,
domain: "localhost",
},
defaultCookieAttributes: {
secure: true,
httpOnly: true,
sameSite: "none",
partitioned: true,
},
},
emailAndPassword: {
enabled: true,
},
session: {
cookieCache: {
enabled: true,
}
}
});

export async function signUp(email: string, password: string, image: string | null = null) {
const result = await auth.api.signUpEmail({
body: {
name: "User Name",
email,
password,
image,
callbackURL: "/setup"
},
});

return result;
}

export async function signIn(email: string, password: string) {
return await auth.api.signInEmail({
body: {
email,
password,
callbackURL: "/setup"
}
});
}

export async function signOut() {
await auth.api.signOut({
headers: []
});
}
import { betterAuth } from 'better-auth';
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from '../../prismdb/prismdb';

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
trustedOrigins: [`localhost`],
advanced: {
database: { useNumberId: true },
crossSubDomainCookies: {
enabled: true,
domain: "localhost",
},
defaultCookieAttributes: {
secure: true,
httpOnly: true,
sameSite: "none",
partitioned: true,
},
},
emailAndPassword: {
enabled: true,
},
session: {
cookieCache: {
enabled: true,
}
}
});

export async function signUp(email: string, password: string, image: string | null = null) {
const result = await auth.api.signUpEmail({
body: {
name: "User Name",
email,
password,
image,
callbackURL: "/setup"
},
});

return result;
}

export async function signIn(email: string, password: string) {
return await auth.api.signInEmail({
body: {
email,
password,
callbackURL: "/setup"
}
});
}

export async function signOut() {
await auth.api.signOut({
headers: []
});
}
6 Replies
krisna
krisna2mo ago
which framework did you use? Next.js ?
Ken
KenOP2mo ago
My apologies I thought I added that in my post, I am using Astro I made a session endpoint but this just returns false meaning null session. src/pages/api/auth/session.ts
import type { APIRoute } from 'astro';
import { auth } from '../../../../src/auth/utils/auth';

export const GET: APIRoute = async ({ request }) => {
try {

console.log('Incoming request headers:', { headers: request.headers });

const sess = await auth.api.getSession({ headers: request.headers });


if (!sess) {
return new Response(JSON.stringify({ authenticated: false }), { status: 200, headers: { 'Content-Type': 'application/json' } });
}
return new Response(JSON.stringify({ authenticated: true, user: sess.user, session: sess.session }), { status: 200, headers: { 'Content-Type': 'application/json' } });
} catch (e) {
console.error('Session endpoint error', e);
return new Response(JSON.stringify({ authenticated: false, error: 'Session check failed' }), { status: 500, headers: { 'Content-Type': 'application/json' } });
}
};
import type { APIRoute } from 'astro';
import { auth } from '../../../../src/auth/utils/auth';

export const GET: APIRoute = async ({ request }) => {
try {

console.log('Incoming request headers:', { headers: request.headers });

const sess = await auth.api.getSession({ headers: request.headers });


if (!sess) {
return new Response(JSON.stringify({ authenticated: false }), { status: 200, headers: { 'Content-Type': 'application/json' } });
}
return new Response(JSON.stringify({ authenticated: true, user: sess.user, session: sess.session }), { status: 200, headers: { 'Content-Type': 'application/json' } });
} catch (e) {
console.error('Session endpoint error', e);
return new Response(JSON.stringify({ authenticated: false, error: 'Session check failed' }), { status: 500, headers: { 'Content-Type': 'application/json' } });
}
};
For future readers, I ended up trying out Clerk for authentication and that worked, thanks
pits.dev
pits.dev2mo ago
check if the cookie better-auth.session_token has been set, if it does not exist you will always get null back
Ken
KenOP2mo ago
I tried seting that as well and got null still sadly.
pits.dev
pits.dev2mo ago
Try setting secure: false in defaultCookieAttributes: { secure: true, httpOnly: true, sameSite: "none", partitioned: true, }, since you are working in dev mode.
krisna
krisna4w ago
Based on the first screenshot, looks like you are using the server API which means you don't need to setup API endpoint. You need to understand there a 2 way to use better-auth, client and server. If you were logged in using server and accessing the session using the client, it will return empty

Did you find this page helpful?