GetSession not returning user and/or session objects, here’s details
UPDATE: signInEmail is successfully making database calls, but the getSession is not. It’s a svelte5/sveltekit project implementing authentication with better-auth. Route protection implemented in routes/+hooks.server.ts, routes/login/+page.server.ts is successfully logging in and creating a valid (I believe) cookie. Here’s code and logged results. My apologies for all the logs, I’m a novice at everything so just trying to work my way through this. Wish it worked like the better-auth documentation for svelte.
See attached PDF for code and logs. I tried numerous approaches suggested by AI tools and people to extract data from the "body" of the response object but not successful. The <pending> in the response from getSession using response.json() makes me wonder if the database isn't responding but shouldn't the console.log not process given the "await" on the getSession. And, the signInEmail updates the database perfectly.
auth.ts file
export const auth = betterAuth({
database: drizzleAdapter(dbAUTH, {
provider: 'sqlite',
schema: { user, session, account, verification }
}),
session: {
strategy: 'database',
// storeSessionInDatabase: true,
// preserveSessionInDatabase: true,
secret: process.env.AUTH_SECRET,
expiresIn: 30 * 24 * 60 * 60,
freshAge: 60 * 5,
cookieCache: {
maxAge: 60 * 60 * 24 * 30,
enabled: true
},
cookie: {
name: 'better-auth.session_token',
// path: '/',
// maxAge: 60 * 60 * 24 * 30, // 30 days
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
sameSite: 'lax'
}
},
emailAndPassword: {
enabled: true,
async onSignIn({ user, session }: { user: User; session: Session }) {
console.log('auth - onSignIn - user', user, 'session', session);
return { session, user };
}
},
See attached PDF for code and logs. I tried numerous approaches suggested by AI tools and people to extract data from the "body" of the response object but not successful. The <pending> in the response from getSession using response.json() makes me wonder if the database isn't responding but shouldn't the console.log not process given the "await" on the getSession. And, the signInEmail updates the database perfectly.
auth.ts file
export const auth = betterAuth({
database: drizzleAdapter(dbAUTH, {
provider: 'sqlite',
schema: { user, session, account, verification }
}),
session: {
strategy: 'database',
// storeSessionInDatabase: true,
// preserveSessionInDatabase: true,
secret: process.env.AUTH_SECRET,
expiresIn: 30 * 24 * 60 * 60,
freshAge: 60 * 5,
cookieCache: {
maxAge: 60 * 60 * 24 * 30,
enabled: true
},
cookie: {
name: 'better-auth.session_token',
// path: '/',
// maxAge: 60 * 60 * 24 * 30, // 30 days
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
sameSite: 'lax'
}
},
emailAndPassword: {
enabled: true,
async onSignIn({ user, session }: { user: User; session: Session }) {
console.log('auth - onSignIn - user', user, 'session', session);
return { session, user };
}
},