[GH-ISSUE #768] session databaseHooks and additionalFields not working with secondary storage #25743

Closed
opened 2026-04-17 16:00:46 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @Roronoa-Zoroo on GitHub (Dec 4, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/768

Using redis as secondary storage.
Added activeOrganizationId but its not reflecting.
when removing secondary storage it works.

betterAuth({
  session: {
    additionalFields: { testField: { type: "string", defaultValue: "hello" } },
  },
  databaseHooks: {
    session: {
      create: {
        before: async (session) => {
          return {
            data: {
              ...session,
              activeOrganizationId: new ObjectId("6749edf62ddd00fa91c19dd5"),
            },
          };
        },
      },
    },
  },
  database: mongodbAdapter(papr.db!),
  emailAndPassword: {
    enabled: true,
  },
  rateLimit: {
    storage: "secondary-storage",
  },
  secondaryStorage: {
    get: async (key) => {
      const resp = await redis.get(key);
      if (resp) {
        return JSON.parse(resp);
      }
      return resp;
    },
    set: async (key, value, ttl) => {
      if (ttl) await redis.set(key, JSON.stringify(value), "EX", ttl);
      else await redis.set(key, JSON.stringify(value));
    },
    delete: async (key) => {
      await redis.del(key);
    },
  },
  plugins: [organization()],
  trustedOrigins: [ENV.CLIENT_URL],
});

The hook is being called just the data is not reflected

Originally created by @Roronoa-Zoroo on GitHub (Dec 4, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/768 Using redis as secondary storage. Added activeOrganizationId but its not reflecting. when removing secondary storage it works. ```js betterAuth({ session: { additionalFields: { testField: { type: "string", defaultValue: "hello" } }, }, databaseHooks: { session: { create: { before: async (session) => { return { data: { ...session, activeOrganizationId: new ObjectId("6749edf62ddd00fa91c19dd5"), }, }; }, }, }, }, database: mongodbAdapter(papr.db!), emailAndPassword: { enabled: true, }, rateLimit: { storage: "secondary-storage", }, secondaryStorage: { get: async (key) => { const resp = await redis.get(key); if (resp) { return JSON.parse(resp); } return resp; }, set: async (key, value, ttl) => { if (ttl) await redis.set(key, JSON.stringify(value), "EX", ttl); else await redis.set(key, JSON.stringify(value)); }, delete: async (key) => { await redis.del(key); }, }, plugins: [organization()], trustedOrigins: [ENV.CLIENT_URL], }); ``` The hook is being called just the data is not reflected
GiteaMirror added the locked label 2026-04-17 16:00:46 -05:00
Author
Owner

@Bekacru commented on GitHub (Dec 5, 2024):

Could you try converting the id to be a string instead of an object ID? The adapter will already attempt to convert it to an object ID before storing it in Mongo but this would fail on secondary storage.

<!-- gh-comment-id:2519596204 --> @Bekacru commented on GitHub (Dec 5, 2024): Could you try converting the id to be a string instead of an object ID? The adapter will already attempt to convert it to an object ID before storing it in Mongo but this would fail on secondary storage.
Author
Owner

@Roronoa-Zoroo commented on GitHub (Dec 5, 2024):

Tried with string still not working

<!-- gh-comment-id:2520432176 --> @Roronoa-Zoroo commented on GitHub (Dec 5, 2024): Tried with string still not working
Author
Owner

@DiPSoMaNiE commented on GitHub (Dec 16, 2024):

Same issue with postgres + prisma adapter: the hook is called but the session stored in redis is missing the activeOrganizationId field. Removing the secondary storage makes it work as expected.

<!-- gh-comment-id:2545579959 --> @DiPSoMaNiE commented on GitHub (Dec 16, 2024): Same issue with postgres + prisma adapter: the hook is called but the session stored in redis is missing the `activeOrganizationId` field. Removing the secondary storage makes it work as expected.
Author
Owner

@ISnackable commented on GitHub (Dec 22, 2024):

I have the same issue, as mentioned, removing the secondary storage works as expected.

<!-- gh-comment-id:2558586539 --> @ISnackable commented on GitHub (Dec 22, 2024): I have the same issue, as mentioned, removing the secondary storage works as expected.
Author
Owner

@DiPSoMaNiE commented on GitHub (Dec 23, 2024):

Can confirm this now works in v1.1.3, thanks @Bekacru

<!-- gh-comment-id:2560403191 --> @DiPSoMaNiE commented on GitHub (Dec 23, 2024): Can confirm this now works in v1.1.3, thanks @Bekacru
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25743