Missing id Property in Session Object When Using Secondary Storage #917

Closed
opened 2026-03-13 08:09:49 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Phillip9587 on GitHub (Mar 26, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Configure Better Auth with a secondary storage implementation, such as Redis:​ https://www.better-auth.com/docs/concepts/database#secondary-storage
  2. Authenticate a user to create a session
  3. Retrieve the session object.

Current vs. Expected behavior

Expected Behavior: The session object should include the id field, as specified in the docs https://www.better-auth.com/docs/concepts/database#session.​

Current Behavior: The id property is missing from the session object when secondary storage is used.

What version of Better Auth are you using?

1.2.4 and 1.2.5-beta.10

Provide environment information

- OS: Ubuntu
- Browsers: Chrome and Firefox
- Node.js: v22.14.0

Which area(s) are affected? (Select all that apply)

Backend, Client

Auth config (if applicable)

import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { admin, multiSession, organization } from 'better-auth/plugins';
import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';

export const auth = betterAuth({
  account: {
    accountLinking: {
      enabled: true,
    },
  },
  emailAndPassword: {
    enabled: true,
  },
  plugins: [admin(), organization(), multiSession()],
  database: drizzleAdapter(drizzle(new Database()), {
    provider: 'sqlite',
    usePlural: true,
  }),
  secret: env.BETTER_AUTH_SECRET,
  socialProviders: {
    google: {
      clientId: env.GOOGLE_CLIENT_ID,
      clientSecret: env.GOOGLE_CLIENT_SECRET,
    },
    microsoft: {
      clientId: env.MICROSOFT_CLIENT_ID,
      clientSecret: env.MICROSOFT_CLIENT_SECRET,
      tenantId: 'common',
      requireSelectAccount: true,
    },
    github: {
      clientId: env.GITHUB_CLIENT_ID,
      clientSecret: env.GITHUB_CLIENT_SECRET,
    },
  },
  secondaryStorage: {
    get: async (key) =>
        await env.BETTER_AUTH_SECONDARY_STORAGE.get(key, { type: 'text' }),
    set: async (key, value, ttl) =>
        await env.BETTER_AUTH_SECONDARY_STORAGE.put(key, value, {
          expirationTtl: ttl,
        }),
    delete: async (key) =>
        await env.BETTER_AUTH_SECONDARY_STORAGE.delete(key),
  },
});

Additional context

No response

Originally created by @Phillip9587 on GitHub (Mar 26, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Configure Better Auth with a secondary storage implementation, such as Redis:​ https://www.better-auth.com/docs/concepts/database#secondary-storage 2. Authenticate a user to create a session 3. Retrieve the session object. ### Current vs. Expected behavior **Expected Behavior:** The session object should include the `id` field, as specified in the docs https://www.better-auth.com/docs/concepts/database#session.​ **Current Behavior:** The `id` property is missing from the session object when secondary storage is used. ### What version of Better Auth are you using? 1.2.4 and 1.2.5-beta.10 ### Provide environment information ```bash - OS: Ubuntu - Browsers: Chrome and Firefox - Node.js: v22.14.0 ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; import { drizzleAdapter } from 'better-auth/adapters/drizzle'; import { admin, multiSession, organization } from 'better-auth/plugins'; import Database from 'better-sqlite3'; import { drizzle } from 'drizzle-orm/better-sqlite3'; export const auth = betterAuth({ account: { accountLinking: { enabled: true, }, }, emailAndPassword: { enabled: true, }, plugins: [admin(), organization(), multiSession()], database: drizzleAdapter(drizzle(new Database()), { provider: 'sqlite', usePlural: true, }), secret: env.BETTER_AUTH_SECRET, socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID, clientSecret: env.GOOGLE_CLIENT_SECRET, }, microsoft: { clientId: env.MICROSOFT_CLIENT_ID, clientSecret: env.MICROSOFT_CLIENT_SECRET, tenantId: 'common', requireSelectAccount: true, }, github: { clientId: env.GITHUB_CLIENT_ID, clientSecret: env.GITHUB_CLIENT_SECRET, }, }, secondaryStorage: { get: async (key) => await env.BETTER_AUTH_SECONDARY_STORAGE.get(key, { type: 'text' }), set: async (key, value, ttl) => await env.BETTER_AUTH_SECONDARY_STORAGE.put(key, value, { expirationTtl: ttl, }), delete: async (key) => await env.BETTER_AUTH_SECONDARY_STORAGE.delete(key), }, }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 08:09:49 -05:00
Author
Owner

@peyronoscar commented on GitHub (May 21, 2025):

Settings storeSessionInDatabase: true worked for me

import { betterAuth } from "better-auth";
export const auth = betterAuth({
	session: {
		modelName: "sessions",
		fields: {
			userId: "user_id"
		},
		expiresIn: 604800, // 7 days
		updateAge: 86400, // 1 day
		disableSessionRefresh: true, // Disable session refresh so that the session is not updated regardless of the `updateAge` option. (default: `false`)
		additionalFields: { // Additional fields for the session table
			customField: {
				type: "string",
				nullable: true
			}
		},
		storeSessionInDatabase: true, // Store session in database when secondary storage is provided (default: `false`)
		preserveSessionInDatabase: false, // Preserve session records in database when deleted from secondary storage (default: `false`)
		cookieCache: {
			enabled: true, // Enable caching session in cookie (default: `false`)	
			maxAge: 300 // 5 minutes
		}
	},
})

Docs

@peyronoscar commented on GitHub (May 21, 2025): Settings `storeSessionInDatabase: true` worked for me ```typescript import { betterAuth } from "better-auth"; export const auth = betterAuth({ session: { modelName: "sessions", fields: { userId: "user_id" }, expiresIn: 604800, // 7 days updateAge: 86400, // 1 day disableSessionRefresh: true, // Disable session refresh so that the session is not updated regardless of the `updateAge` option. (default: `false`) additionalFields: { // Additional fields for the session table customField: { type: "string", nullable: true } }, storeSessionInDatabase: true, // Store session in database when secondary storage is provided (default: `false`) preserveSessionInDatabase: false, // Preserve session records in database when deleted from secondary storage (default: `false`) cookieCache: { enabled: true, // Enable caching session in cookie (default: `false`) maxAge: 300 // 5 minutes } }, }) ``` [Docs](https://www.better-auth.com/docs/reference/options#session)
Author
Owner

@ping-maxwell commented on GitHub (Aug 11, 2025):

Hello all, is this still an issue on the latest version of Better-Auth?

@ping-maxwell commented on GitHub (Aug 11, 2025): Hello all, is this still an issue on the latest version of Better-Auth?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#917