Session Cache disappearing in Next.js

Expected behavior:
Cached session refreshes in cookies every 30 seconds

Current behavior:
Cached session disappears after 30 seconds and never gets refreshed, leaving better-auth to default back to bringing the session from the database.

Better-Auth version
v1.1.3 (Latest)

Bug Details:
I started testing out session cache in my project, set the cache to 30 seconds and tried using it.

After logging in, the cached session better-auth.session_data stays in my cookies for 30 seconds and then disappears, never to return. Better-auth then defaults back to bringing the session from the database.

The only way I found to get the cached session back is to update the user, but then same loop happens, wait 30 seconds, disappear without being.

This is some of my auth options for reference. I'm also using the Oragnization plugin
export const auth = betterAuth({
    database: drizzleAdapter(db, { provider: 'pg' }),
    session: { cookieCache: { enabled: true, maxAge: 30 } },
    databaseHooks: {
        session: {
            create: {
                before: async session => {
                    const { data, error: _error } = await getUserOrganizationAuth(session.userId)
                    return { data: { ...session, activeOrganizationId: data?.id } }
                },
            },
        },
    },
})
Was this page helpful?