storeSessionInDatabase and deleteSession Behavior #545

Closed
opened 2026-03-13 07:52:10 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @wh5938316 on GitHub (Jan 10, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When storeSessionInDatabase is enabled alongside secondaryStorage, calling deleteSession results in the corresponding session record being deleted from the primary database as well.

d059d9b618/packages/better-auth/src/db/internal-adapter.ts (L427-L436)

The purpose of enabling storeSessionInDatabase is to retain historical session data in the primary database for future audits or related use cases. However, if signing out deletes the session from the primary database, this defeats the purpose of saving sessions.

Steps to Reproduce
1. Enable both storeSessionInDatabase and secondaryStorage.
2. Sign in to create a session.
3. Call deleteSession to sign out.
4. Observe that the session record is removed from the primary database as well as secondaryStorage.

Current vs. Expected behavior

Expected Behavior

When storeSessionInDatabase is enabled, deleteSession should only remove the session from secondaryStorage but retain it in the primary database for historical purposes.

Observed Behavior

The session record is deleted from both secondaryStorage and the primary database.

What version of Better Auth are you using?

1.1.10

Provide environment information

-

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
  secondaryStorage: ...,
  session: {
    storeSessionInDatabase: true,
  },
});

Additional context

Additional Context

To work around this, I attempted to save the session in the primary database using a session “after hook.” However, I discovered an issue in the internalAdapter.findSession logic. When a session is not found in secondaryStorage (e.g., after deletion), findSession continues to search for it in the primary database, which may lead to unexpected results.

d059d9b618/packages/better-auth/src/db/internal-adapter.ts (L247-L269)

I am currently working on a fix and plan to submit a pull request to resolve this issue.

Originally created by @wh5938316 on GitHub (Jan 10, 2025). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce When storeSessionInDatabase is enabled alongside secondaryStorage, calling deleteSession results in the corresponding session record being deleted from the primary database as well. https://github.com/better-auth/better-auth/blob/d059d9b61870ba5dad6f3e0a86c06a6a3b532940/packages/better-auth/src/db/internal-adapter.ts#L427-L436 The purpose of enabling storeSessionInDatabase is to retain historical session data in the primary database for future audits or related use cases. However, if signing out deletes the session from the primary database, this defeats the purpose of saving sessions. Steps to Reproduce 1. Enable both storeSessionInDatabase and secondaryStorage. 2. Sign in to create a session. 3. Call deleteSession to sign out. 4. Observe that the session record is removed from the primary database as well as secondaryStorage. ### Current vs. Expected behavior ### Expected Behavior When storeSessionInDatabase is enabled, deleteSession should only remove the session from secondaryStorage but retain it in the primary database for historical purposes. ### Observed Behavior The session record is deleted from both secondaryStorage and the primary database. ### What version of Better Auth are you using? 1.1.10 ### Provide environment information ```bash - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, secondaryStorage: ..., session: { storeSessionInDatabase: true, }, }); ``` ### Additional context ### Additional Context To work around this, I attempted to save the session in the primary database using a session “after hook.” However, I discovered an issue in the internalAdapter.findSession logic. When a session is not found in secondaryStorage (e.g., after deletion), findSession continues to search for it in the primary database, which may lead to unexpected results. https://github.com/better-auth/better-auth/blob/d059d9b61870ba5dad6f3e0a86c06a6a3b532940/packages/better-auth/src/db/internal-adapter.ts#L247-L269 I am currently working on a fix and plan to submit a pull request to resolve this issue.
GiteaMirror added the bug label 2026-03-13 07:52:10 -05:00
Author
Owner

@wh5938316 commented on GitHub (Jan 10, 2025):

I observed that the second issue I mentioned earlier seems to be intentional behavior.

d059d9b618/packages/better-auth/src/db/internal-adapter.ts (L298-L317)

However, there are some problems with this approach:

  1. Contradicts the Purpose of secondaryStorage:
    When secondaryStorage is enabled and storeSessionInDatabase is disabled (as it was previously by default), sessions are not stored in the primary database. Therefore, if a session is not found in Redis, it should simply return null instead of falling back to the primary database.

  2. Behavior with storeSessionInDatabase:
    Even when storeSessionInDatabase is enabled, as I mentioned earlier, sessions in the primary database are intended for long-term storage and should not be deleted during sign-out or revocation. If a session is not found in Redis, it should not trigger a query to the primary database, nor should the result be written back to Redis.

  3. Edge Cases with Database Hooks:
    This approach also introduces edge cases. For example, when I enabled databaseHooks.session.before, I added user-specific organization information to the session. However, if a session is not found in Redis, better-auth writes the session from the primary database back into Redis. This causes inconsistencies, as the session data retrieved from the primary database does not match the data originally stored in Redis during a normal signInEmail flow.

These issues could lead to unexpected behavior and conflicts with the intended purpose of secondaryStorage and storeSessionInDatabase.

@wh5938316 commented on GitHub (Jan 10, 2025): I observed that the second issue I mentioned earlier seems to be intentional behavior. https://github.com/better-auth/better-auth/blob/d059d9b61870ba5dad6f3e0a86c06a6a3b532940/packages/better-auth/src/db/internal-adapter.ts#L298-L317 However, there are some problems with this approach: 1. Contradicts the Purpose of secondaryStorage: When secondaryStorage is enabled and storeSessionInDatabase is disabled (as it was previously by default), sessions are not stored in the primary database. Therefore, if a session is not found in Redis, it should simply return null instead of falling back to the primary database. 2. Behavior with storeSessionInDatabase: Even when storeSessionInDatabase is enabled, as I mentioned earlier, sessions in the primary database are intended for long-term storage and should not be deleted during sign-out or revocation. If a session is not found in Redis, it should not trigger a query to the primary database, nor should the result be written back to Redis. 3. Edge Cases with Database Hooks: This approach also introduces edge cases. For example, when I enabled `databaseHooks.session.before`, I added user-specific organization information to the session. However, if a session is not found in Redis, better-auth writes the session from the primary database back into Redis. This causes inconsistencies, as the session data retrieved from the primary database does not match the data originally stored in Redis during a normal signInEmail flow. These issues could lead to unexpected behavior and conflicts with the intended purpose of secondaryStorage and storeSessionInDatabase.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#545