useSession hook calls get-session when logged out (fetchEnabled option) #550

Closed
opened 2026-03-13 07:52:30 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @daveycodez on GitHub (Jan 11, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Use the useSession hook anywhere in a logged out state, and it will still send requests to /api/auth/get-session.

This means all guests that visit the website will send these extra requests. It would be great if useSession only sends request if the user is currently logged in. Sending all these extra requests on Vercel for example will count towards function execution calls which means $$$ cost

What version of Better Auth are you using?

1.1.10

Provide environment information

Next.js

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

Client

Auth config (if applicable)

No response

Additional context

No response

Originally created by @daveycodez on GitHub (Jan 11, 2025). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce Use the useSession hook anywhere in a logged out state, and it will still send requests to /api/auth/get-session. This means all guests that visit the website will send these extra requests. It would be great if useSession only sends request if the user is currently logged in. Sending all these extra requests on Vercel for example will count towards function execution calls which means $$$ cost ### What version of Better Auth are you using? 1.1.10 ### Provide environment information ```bash Next.js ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) _No response_ ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 07:52:30 -05:00
Author
Owner

@mnewlife commented on GitHub (Jan 15, 2025):

I'm encountering this too!

@mnewlife commented on GitHub (Jan 15, 2025): I'm encountering this too!
Author
Owner

@daveycodez commented on GitHub (Jan 18, 2025):

Since it uses http-only cookie there is no way to know, we have to track this manually.

So the new request is to update useSession with the following props:

const useSession = (options?: {fetchEnabled: boolean = true}) => {}

and only call get-session if fetchEnabled is set to true, otherwise only track the $store for session changes.

And yea, default to true

@daveycodez commented on GitHub (Jan 18, 2025): Since it uses http-only cookie there is no way to know, we have to track this manually. So the new request is to update useSession with the following props: ```ts const useSession = (options?: {fetchEnabled: boolean = true}) => {} ``` and only call get-session if fetchEnabled is set to true, otherwise only track the $store for session changes. And yea, default to true
Author
Owner

@benkingcode commented on GitHub (Feb 20, 2025):

Anyone know a workaround for this? Makes it cost-wise unusable for us otherwise

@benkingcode commented on GitHub (Feb 20, 2025): Anyone know a workaround for this? Makes it cost-wise unusable for us otherwise
Author
Owner

@dosubot[bot] commented on GitHub (Jun 14, 2025):

Hi, @daveycodez. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary

  • The useSession hook in the Better Auth library is making unnecessary requests to /api/auth/get-session even when users are logged out.
  • This behavior could potentially increase costs on platforms like Vercel.
  • You suggested adding a fetchEnabled option to the useSession hook to control when requests are made.
  • Other users, including "mnewlife" and "benkingcode," have also reported this issue, with "benkingcode" looking for a workaround due to cost concerns.

Next Steps

  • Please let us know if this issue is still relevant to the latest version of the better-auth repository. If it is, you can keep the discussion open by commenting on this issue.
  • Otherwise, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jun 14, 2025): Hi, @daveycodez. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary** - The `useSession` hook in the Better Auth library is making unnecessary requests to `/api/auth/get-session` even when users are logged out. - This behavior could potentially increase costs on platforms like Vercel. - You suggested adding a `fetchEnabled` option to the `useSession` hook to control when requests are made. - Other users, including "mnewlife" and "benkingcode," have also reported this issue, with "benkingcode" looking for a workaround due to cost concerns. **Next Steps** - Please let us know if this issue is still relevant to the latest version of the better-auth repository. If it is, you can keep the discussion open by commenting on this issue. - Otherwise, this issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#550