[GH-ISSUE #1374] Drizzle adapter (MySQL) uses the wrong field in select from session model in createTransform #8718

Closed
opened 2026-04-13 03:52:55 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @ck-euan on GitHub (Feb 7, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1374

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

The createTransform function attempts to fetch a session by ID.
However, after updating a session createTransform is passed the token instead.
This results in updating the session returning null and the user is then signed out.

You can see this behaviour by:

  1. Use the drizzle adapter with MySQL (I haven't tested other adapters / DBs)
  2. Set a really low updateAge for testing purposes
  3. Wait until the updateAge has passed but the session has not expired
  4. Call auth.api.getSession()
  5. Session will be returned as null

Current vs. Expected behavior

Current behaviour:
The updated session is not found and the user is logged out

Expected behaviour:
The updated session is found and the user remains logged in

What version of Better Auth are you using?

1.1.16

Provide environment information

- OS: Mac OS
- Browser: Arc

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

Backend

Auth config (if applicable)


Additional context

This is particularly noticeable for me because I have expiresIn 30 minutes and updateAge 10 minutes. This bug means that my users get logged out every 10 minutes. The session still gets updated but the getSession api doesn't receive the updatedSession so it deletes the cookies and returns null.

I've patched my better-auth install with this and it seems to resolve the issue. Not sure if this is the best solution:

// adapters/drizzle.js inside the withReturning function
  if (updatedResult) {
         const upId = updatedResult?.queryChunks[3]?.value;
         const schemaModel = getSchema(model);
-        const res = await db.select().from(schemaModel).where(eq(schemaModel.id, upId));
-        return res[0];
+        // BUG: We don't want to use ID for the session model updates because we recieve the `token` here instead
+        if(model === "session"){
+          const res = await db.select().from(schemaModel).where(eq(schemaModel.token, upId));
+          return res[0];
+        }else {
+          const res = await db.select().from(schemaModel).where(eq(schemaModel.id, upId));
+          return res[0];
+        }
Originally created by @ck-euan on GitHub (Feb 7, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1374 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce The createTransform function attempts to fetch a session by ID. However, after updating a session createTransform is passed the token instead. This results in updating the session returning null and the user is then signed out. You can see this behaviour by: 1. Use the drizzle adapter with MySQL (I haven't tested other adapters / DBs) 2. Set a really low updateAge for testing purposes 3. Wait until the updateAge has passed but the session has not expired 4. Call auth.api.getSession() 5. Session will be returned as null ### Current vs. Expected behavior Current behaviour: The updated session is not found and the user is logged out Expected behaviour: The updated session is found and the user remains logged in ### What version of Better Auth are you using? 1.1.16 ### Provide environment information ```bash - OS: Mac OS - Browser: Arc ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context This is particularly noticeable for me because I have expiresIn 30 minutes and updateAge 10 minutes. This bug means that my users get logged out every 10 minutes. The session still gets updated but the getSession api doesn't receive the updatedSession so it deletes the cookies and returns null. I've patched my better-auth install with this and it seems to resolve the issue. Not sure if this is the best solution: ``` // adapters/drizzle.js inside the withReturning function if (updatedResult) { const upId = updatedResult?.queryChunks[3]?.value; const schemaModel = getSchema(model); - const res = await db.select().from(schemaModel).where(eq(schemaModel.id, upId)); - return res[0]; + // BUG: We don't want to use ID for the session model updates because we recieve the `token` here instead + if(model === "session"){ + const res = await db.select().from(schemaModel).where(eq(schemaModel.token, upId)); + return res[0]; + }else { + const res = await db.select().from(schemaModel).where(eq(schemaModel.id, upId)); + return res[0]; + } ```
GiteaMirror added the lockedbug labels 2026-04-13 03:52:55 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8718