❓ Help with better-auth: cookieCache not persisting after maxAge expires

Hey everyone, I'm using better-auth and I have cookieCache enabled (true) with maxAge set to 60 * 1 (1 minute). When I first log in, two cookies are set: better-auth.session_token better-auth.session_data When I call the API route api/auth/get-session, the session is correctly read from the cookie, and no DB query is made (confirmed using Drizzle's query logger in the console). However, after maxAge expires: The better-auth.session_data cookie is destroyed. A DB query is made to fetch session data (which is expected). But the session_data cookie is not re-set, so on every subsequent request, it keeps querying the DB again and again. Is this a bug in better-auth? Or am I missing something in the setup? Would appreciate any help or clarification on this! 🙏 Would you like me to help you debug this behavior or write a GitHub issue template for it as well?
12 Replies
Andrei
Andrei5mo ago
I'm experiencing the same but only when signed in with social providers, it works fine with email/password
Karthickeyan
KarthickeyanOP5mo ago
Yes, I also use social providers only (google oauth)
Andrei
Andrei5mo ago
I double checked it yesterday with Microsoft, same as with Google - no session server calls at all @Ping could you assist, please. Is it a bug in a Vue package? I presume that's because it's not making any calls to the server when logged in with any social provider, I described the behavior here (with video): https://discord.com/channels/1288403910284935179/1370151660416012498
mrk
mrk3mo ago
Hey @Andrei did you manage to solve this? I am having the same issue
Andrei
Andrei3mo ago
What framework are you using? React or Vue based?
mrk
mrk3mo ago
Heey, thanks for the quick reply... I am using nuxt, so vue-based, but in SPA mode (ssr: false), so its basically a regular vue app
session: {
cookieCache: {
enabled: true,
maxAge: 10 * 60, // 10 minutes
},
}
session: {
cookieCache: {
enabled: true,
maxAge: 10 * 60, // 10 minutes
},
}
Andrei
Andrei3mo ago
I had a lot of issues with Nuxt and had to migrate to Next.js The bad news - with Next everything works fine and fast
mrk
mrk3mo ago
Oh, that's unfortunate, I've enjoy nuxt for all its other benefits but can agree, getting it to work properly with better-auth has been a pain.
Andrei
Andrei3mo ago
Also, on Cloudflare it didn’t work at all with better auth, I kept having 522 errors
mrk
mrk3mo ago
Oh yeah, that was especially painful. I ended up solving the cloudflare workers/pages issue by using a wrapper function to define my instance in server/utils/auth.ts (this does not conflict with cloudflare isolates booting up):
export const serverAuth = () => {
return betterAuth({...})
}
export const serverAuth = () => {
return betterAuth({...})
}
and then having a separate server/utils/auth.cli.ts for the better-auth CLI which exports the auth instance by doing:
import { serverAuth } from './auth'

export const auth = serverAuth()
import { serverAuth } from './auth'

export const auth = serverAuth()
which also works for running the better-auth CLI e.g. for db auth schema migrations. Also, I had to use cloudflare hyperdrive for this, since otherwise I would ran out of connection slots (I am using supabase, which hast supavisor, an alternative to cloudflare hypervisor, but that didn't work for me somehow)
Andrei
Andrei3mo ago
I did all of that but still experienced issues 522 something And databaseHooks from better auth didn’t work for me
Xafets
Xafets2w ago
Did anyone end up solving this? I'm running into this now

Did you find this page helpful?