[GH-ISSUE #801] get-session endpoint returning 200 on error #8443

Closed
opened 2026-04-13 03:30:50 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @awesomepandapig on GitHub (Dec 7, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/801

Currently when querying /api/auth/get-session without a valid session token returns null with a 200 response status.

I believe that this endpoint should instead return a 400 error indicating that the request is missing a valid token, or a 401/403 if the token is malformed or not valid for the particular resource.

Without proper error codes validation is much more difficult.

Originally created by @awesomepandapig on GitHub (Dec 7, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/801 Currently when querying `/api/auth/get-session` without a valid session token returns `null` with a `200` response status. I believe that this endpoint should instead return a `400` error indicating that the request is missing a valid token, or a `401/403` if the token is malformed or not valid for the particular resource. Without proper error codes validation is much more difficult.
GiteaMirror added the locked label 2026-04-13 03:30:50 -05:00
Author
Owner

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

We're returning null to stay consistent with auth.api.getSession. Users won’t expect this to throw an error since it's primarily used in middleware or other contexts where you need to check if a session exists. Plus, throwing an error would generate unnecessary logs if you're using monitoring services for expected behavior. On the client, you can check if isPending is false and data is still null to determine whether the session was found.

<!-- gh-comment-id:2524990614 --> @Bekacru commented on GitHub (Dec 7, 2024): We're returning `null` to stay consistent with `auth.api.getSession`. Users won’t expect this to throw an error since it's primarily used in middleware or other contexts where you need to check if a session exists. Plus, throwing an error would generate unnecessary logs if you're using monitoring services for expected behavior. On the client, you can check if `isPending` is `false` and `data` is still `null` to determine whether the session was found.
Author
Owner

@awesomepandapig commented on GitHub (Dec 7, 2024):

Hmmm... I see. I am using express as my backend so I was thinking more in terms of an explicit client-server model where the server would be passing explicit error messages to the client if the session is invalid or DNE.

For now, though, data being null works great with my Next.JS middleware.

<!-- gh-comment-id:2524992157 --> @awesomepandapig commented on GitHub (Dec 7, 2024): Hmmm... I see. I am using express as my backend so I was thinking more in terms of an explicit client-server model where the server would be passing explicit error messages to the client if the session is invalid or DNE. For now, though, data being null works great with my Next.JS middleware.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8443