[GH-ISSUE #280] Session model userId rename causes errors on session checks #8200

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

Originally created by @wsngamerz on GitHub (Oct 21, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/280

Describe the bug
Renaming the userId field on the session model leads to all session validation checks returning a 401 status, even when logins are successful. No visible console errors are present, other than the 401 response from /api/auth/session.

To Reproduce
Steps to reproduce the behavior:

  1. Setup better-auth, renaming the userId field:
export const auth = betterAuth({
   // ...
    session: {
        fields: {
            userId: "user_id",
        }
    }
})
  1. Attempt to login
  2. The login succeeds, but as soon as /api/auth/session is called, it returns a 401 error, indicating that the session is not maintained. For instance, on the demo site, this results in an endless loop redirecting back to the login page after each login attempt.

Expected behavior
Being able to rename the userId field and still be able to remain logged in.

Desktop (please complete the following information):

  • OS: Pop!_OS 24.04 LTS
  • Browser: Chrome 129.0 & firefox 131.0
  • Version: better-auth v0.5.3-beta.2

Additional context
The issue was reproduced both in isolation and within the demo/nextjs project. No console errors are shown aside from the 401 status response from /api/auth/session.

Originally created by @wsngamerz on GitHub (Oct 21, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/280 **Describe the bug** Renaming the `userId` field on the session model leads to all session validation checks returning a 401 status, even when logins are successful. No visible console errors are present, other than the 401 response from `/api/auth/session`. **To Reproduce** Steps to reproduce the behavior: 1. Setup `better-auth`, renaming the `userId` field: ```ts export const auth = betterAuth({ // ... session: { fields: { userId: "user_id", } } }) ``` 2. Attempt to login 3. The login succeeds, but as soon as `/api/auth/session` is called, it returns a 401 error, indicating that the session is not maintained. For instance, on the demo site, this results in an endless loop redirecting back to the login page after each login attempt. **Expected behavior** Being able to rename the `userId` field and still be able to remain logged in. **Desktop (please complete the following information):** - OS: Pop!_OS 24.04 LTS - Browser: Chrome 129.0 & firefox 131.0 - Version: better-auth v0.5.3-beta.2 **Additional context** The issue was reproduced both in isolation and within the `demo/nextjs` project. No console errors are shown aside from the 401 status response from `/api/auth/session`.
GiteaMirror added the locked label 2026-04-13 03:18:19 -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#8200