useSession not reactive?
The useSession hook from the authentication client is not updating reactively. After the initial render, the isPending state remains true indefinitely, even after authentication is complete. The component only refreshes its state when there's a code change that forces a re-render.
const { data: session, isPending } = authClient.useSession();
Even after authentication is complete, the component doesn't update to show the user as logged in until the file is saved or another action triggers a re-render.
34 Replies
This is issue a lot of people face: https://github.com/better-auth/better-auth/issues/1006
GitHub
useSession()
not always triggering a state change · Issue #1006 ·...Is this suited for github? Yes, this is suited for github To Reproduce Sign in with email and password Use useSession() in the following component: export function AuthLayout({ children }: AppLayou...
Solution is to replace authClient.useSession() with custom hook useAuthSession():
import { authClient } from "@/lib/auth-client";
import { useEffect, useRef } from "react";
function useAuthSession() {
const {
data,
isPending,
error, //error object
refetch,
} = authClient.useSession();
// Track the latest value of isPending
const isPendingRef = useRef(isPending);
useEffect(() => {
isPendingRef.current = isPending;
}, [isPending]);
useEffect(() => {
if (isPending) {
const timerNotify = setTimeout(() => {
if (isPendingRef.current) {
refetch(); // sends another get-session request to the server
//authClient.$store.notify("$sessionSignal"); // or this poke store to trigger a re-render
}
}, 2500); // after 2.5 seconds of pending, force resetting client store
const timerReload = setTimeout(() => {
if (isPendingRef.current) {
window.location.reload();
}
}, 5000); // after 5 seconds of pending, reload the page
return () => {
clearTimeout(timerNotify);
clearTimeout(timerReload);
};
}
}, [isPending]);
return {
data,
isPending,
error,
};
}
export default useAuthSession;
pff.... not sure I want to do this "trick"...
this is actually quite a fundamental part of the library, and I am now a bit concerned about it being buggy. ðŸ«
We know it's a fundamental part of the library, and we're trying to figure out what the issue is but still couldn't reproduce it on our end. None of the people have provided a reproducible example I could look at yet. If you can create one, I'd love to take a look.
we're facing the same issue using authclient only when signing up with credentials. @bekacru I saw you posted a client-only demo here is there a way to see the code? maybe that will help us solve our issue. P.S. using google "sing up" updates my session correctly.
GitHub
useSession()
not always triggering a state change · Issue #1006 ·...Is this suited for github? Yes, this is suited for github To Reproduce Sign in with email and password Use useSession() in the following component: export function AuthLayout({ children }: AppLayou...
I know I know. Indeed I tried to reproduced it for you and got no issues what-so-ever, but it's still a problem that it happens...!
GitHub
better-auth/demo/nextjs/app/client-test at main · better-auth/bette...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
@bekacru thanks! I see you have client.useSession() inside the same component you are calling signIn.email. In my case I have a header component inside my layout that calls client.useSession(), and signIn.email() is called in a component inside a specific page. If you have time, can you try a similar structure to see if you can reproduce the bug?
https://github.com/better-auth/better-auth/blob/fix/use-session/demo/nextjs/app/client-test/page.tsx
https://github.com/better-auth/better-auth/blob/fix/use-session/demo/nextjs/components/wrapper.tsx#L21
tried to refactor it. it's still working.
GitHub
better-auth/demo/nextjs/app/client-test/page.tsx at fix/use-session...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
better-auth/demo/nextjs/components/wrapper.tsx at fix/use-session ·...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
is this new version deployed here? https://demo.better-auth.com/client-test If it is, I'm not seeing the avatar component in the header when I log in, which means that <UserAvatar /> is returning null
No it's not deployed. Try to clone the repo and run it locally.
Hi there!
any update regarding the useSession bug?
not really. but the moment someone provides me a reproduction it'll be fixed :))
I was just creating a "boilerplate" for myself and I am getting the error there. Is a bit heavy, but on lighter tries to reproduce it...it was just not happening. Somehow it gets messed up when the codebase is bigger.
I can share it if that's ok
just... know that is not a "minimal reproduction" (but it's just a boilerplate)
yeah sure that works
https://github.com/RicSala/trabajo-veterinaria.git
here is the repo
I have cleaned a bit because I am using the "better-auth-ui" package too, and everything was a bit "mingled"
but I think it's clear enough
try to change organizations several times back and forth, and you will see that the useSession hook randome gets stuck in a previous render
Thanks! Will let you know.
couldn't still recreate it :((( could you please send me a screen recording of what happens?
I was about to do it yesterday.
It does not ALWAYS happen, but it happens often enough to be a problem. let me record that.
there you go @bekacru https://screen.studio/share/SmhFluPR
Area — Screen Studio
Area — Created and shared with Screen Studio
I just create a component to switch organizations
and as you can see, the session is not updating
and it's just printing the session as it comes from the hook:

hope it helps
oh didn't know this was connected to the org plugin. the issue here is changing org doesn't trigger session refetch. It was jsut assumed the user is using
useActiveOrganization
instead. And it only triggers the active session refetch. Someone also pointed this out to me and I updated it earlier today. Update to 1.2.5-beta.4
this should be fixed.
But Im still not sure if this is the same issue mentioned on the gh issue threadI would say it's not, as in previous ocasions I think I was not using th org plugin...
Will keep an eye on it and come back here whenever I spot it again
I just tried 1.2.5-beta.4 and the bug is still there, I'll try to create a small POC to see if you can reproduce it
I added you as a collaborator in my repo, it's called better-auth-poc. Check your github
could you make it public? and also would appreciate a screen recording
just made it public and added the screen recording to the readme
link?
check your DM
Just figured out the root issue here. Thanks for the repro by @mediacrafters It's due to object reference and destructuring. If you try to use a destructured object from the auth client, you need to use the destructured useSession instead of authClient.useSession and vice versa.
This is big news!!!
I was using this across the code base , but still had the same problem
Currenly my fix - calling
refetch
in root wrapper (seems to work)
Hey guys, do we have a propper fix for this issue ? My session is still not updating when i sign in/ sign out in my next js app...
have you checked the above solution. making sure if you're distructing the authclient, you should always keep distructing