Bug: authClient.getSession() does not update client store #421

Closed
opened 2026-03-13 07:45:14 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @JosipPardon on GitHub (Dec 18, 2024).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I am not sure if this should be considered a bug, but authClient.getSession() should update the session store while also returning the new data. See the comments in the code below:

function Component() {

  const session = authClient.useSession();
  console.log("component rendered with session: ", session);

  return (
    <button
      onClick={async () => {

        const result = (await (
          await fetch(`/api/auth/delete-user/callback?token=....`)
        ).json()) as { success?: boolean };

        if (result.success) {
          const session = await authClient.getSession();
          console.log("new session: ", session);
          // session has changed to {data: null, error: null}
          // useSession should now trigger a component re-render, but it does not
        }

      }}
    >
      Delete account
    </button>
  );
}

Current vs. Expected behavior

Currently, authClient.getSession() only returns data, but it should also update the store so that components using useSession are re-rendered when the data changes.

What version of Better Auth are you using?

1.0.21

Provide environment information

Windows 11
Microsoft Edge

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

Client

Originally created by @JosipPardon on GitHub (Dec 18, 2024). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce I am not sure if this should be considered a bug, but `authClient.getSession()` should update the session store while also returning the new data. See the comments in the code below: ```tsx function Component() { const session = authClient.useSession(); console.log("component rendered with session: ", session); return ( <button onClick={async () => { const result = (await ( await fetch(`/api/auth/delete-user/callback?token=....`) ).json()) as { success?: boolean }; if (result.success) { const session = await authClient.getSession(); console.log("new session: ", session); // session has changed to {data: null, error: null} // useSession should now trigger a component re-render, but it does not } }} > Delete account </button> ); } ``` ### Current vs. Expected behavior Currently, `authClient.getSession()` only returns data, but it should also update the store so that components using `useSession` are re-rendered when the data changes. ### What version of Better Auth are you using? 1.0.21 ### Provide environment information ```bash Windows 11 Microsoft Edge ``` ### Which area(s) are affected? (Select all that apply) Client
GiteaMirror added the bug label 2026-03-13 07:45:14 -05:00
Author
Owner

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

We're going to add refetch support to useSession soon. Currently, triggering an update for the store causes a refetch. This means if getSession triggers useSession to update, it also sends a request to update itself. For now, you can use authClient.$store.notify("$sessionSignal"). It's not the prettiest API, but we should have something better soon.

@Bekacru commented on GitHub (Dec 18, 2024): We're going to add refetch support to `useSession` soon. Currently, triggering an update for the store causes a refetch. This means if `getSession` triggers `useSession` to update, it also sends a request to update itself. For now, you can use `authClient.$store.notify("$sessionSignal")`. It's not the prettiest API, but we should have something better soon.
Author
Owner

@JosipPardon commented on GitHub (Dec 18, 2024):

Thank you very much!

@JosipPardon commented on GitHub (Dec 18, 2024): Thank you very much!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#421