Better Auth Error Session data is too large to store in the cookie

I'm always getting this error whenever I tried to sign up.
# SERVER_ERROR: [Error [BetterAuthError]: Session data is too large to store in the cookie. Please disable session cookie caching or reduce the size of the session data] {
cause: undefined
}
# SERVER_ERROR: [Error [BetterAuthError]: Session data is too large to store in the cookie. Please disable session cookie caching or reduce the size of the session data] {
cause: undefined
}
Here's my auth instance
import { db } from '@/db' // your drizzle instance
import { account, session, user, verification } from '@/db/schema/auth'

import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: 'pg', // or "mysql", "sqlite"
schema: {
user,
account,
session,
verification
}
}),
emailAndPassword: {
enabled: true,
async sendResetPassword(data, request) {
// Send an email to the user with a link to reset their password
}
},
advanced: {
generateId: false // Disable default id generation
}
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60 // Cache duration in seconds
}
}
})
import { db } from '@/db' // your drizzle instance
import { account, session, user, verification } from '@/db/schema/auth'

import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: 'pg', // or "mysql", "sqlite"
schema: {
user,
account,
session,
verification
}
}),
emailAndPassword: {
enabled: true,
async sendResetPassword(data, request) {
// Send an email to the user with a link to reset their password
}
},
advanced: {
generateId: false // Disable default id generation
}
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60 // Cache duration in seconds
}
}
})
Solution:
This occurs because the base64 of the profile picture is too large. You should upload the profile picture to a S3 bucket or cloudflare R2 and save only the id to the database / cookie
Jump to solution
3 Replies
Solution
Valardir
Valardir3w ago
This occurs because the base64 of the profile picture is too large. You should upload the profile picture to a S3 bucket or cloudflare R2 and save only the id to the database / cookie
craftzcode
craftzcodeOP3w ago
thank you so much, how did you know the profile picture is causing of this error? because you are correct I just converting the image into base64 for testing
Valardir
Valardir3w ago
Once had the same issue, glad I could help you. Base64 is maybe the only reason a cookie can exceed the max size of 4096 bytes so it was likely the same issues what I had in the past. I recommend cloudflare btw. Just nice and for most use cases for free

Did you find this page helpful?