[GH-ISSUE #522] Session ID Type Conflict and Unexpected Database Query on Secondary Storage Cache Clear #8305

Closed
opened 2026-04-13 03:22:46 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @wh5938316 on GitHub (Nov 14, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/522

Describe the bug
When enabling Secondary storage in better-auth, session IDs default to nanoid, even when database.generateId is set to uuid or false. This setup works as expected, but an issue arises if the Secondary storage (e.g., Redis) is cleared. In this case, better-auth makes a request to the session table in the primary database. If PostgreSQL is used with session.id as a UUID type, it raises a type error due to invalid UUID input syntax, for instance:

invalid input syntax for type uuid: "QvkAB7iB9Qd4w1GnD31lIpJxH0yYQqcn"

After enabling Secondary storage, I noticed that sessions are not stored in the primary database. If making a query to the session table is intended, it seems incorrect that no session record is found in the table after login. If the absence of session records in the primary database after login is expected behavior, better-auth should not attempt to query the database when the session record is missing from Secondary storage.

Expected behavior

  1. If querying the session table in the primary database is intentional, the session should persist in the database after login.
  2. If session persistence in the primary database is not expected after login, better-auth should avoid querying the database when no session record is found in Secondary storage.

Thank you for looking into this!

Originally created by @wh5938316 on GitHub (Nov 14, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/522 **Describe the bug** When enabling Secondary storage in better-auth, session IDs default to nanoid, even when `database.generateId` is set to uuid or false. This setup works as expected, but an issue arises if the Secondary storage (e.g., Redis) is cleared. In this case, better-auth makes a request to the session table in the primary database. If PostgreSQL is used with `session.id` as a UUID type, it raises a type error due to invalid UUID input syntax, for instance: ``` invalid input syntax for type uuid: "QvkAB7iB9Qd4w1GnD31lIpJxH0yYQqcn" ``` After enabling Secondary storage, I noticed that sessions are not stored in the primary database. If making a query to the session table is intended, it seems incorrect that no session record is found in the table after login. If the absence of session records in the primary database after login is expected behavior, better-auth should not attempt to query the database when the session record is missing from Secondary storage. **Expected behavior** 1. If querying the session table in the primary database is intentional, the session should persist in the database after login. 2. If session persistence in the primary database is not expected after login, better-auth should avoid querying the database when no session record is found in Secondary storage. Thank you for looking into this!
GiteaMirror added the locked label 2026-04-13 03:22:46 -05:00
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

I don't think this is an issue anymore since we're doing the lookup by "token" instead. Feel free to reopen if you think it's still applicable. And looking up the db as a fallback makes sense, so if you enable secondary storage at some point, all the existing db-stored sessions won't return null

<!-- gh-comment-id:2501786152 --> @Bekacru commented on GitHub (Nov 26, 2024): I don't think this is an issue anymore since we're doing the lookup by "token" instead. Feel free to reopen if you think it's still applicable. And looking up the db as a fallback makes sense, so if you enable secondary storage at some point, all the existing db-stored sessions won't return null
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8305