Better AuthBA
Better Auth3mo ago
1 reply
Max

How do I force the useSession hook to update when a user's data changes independent of the session?

I'm using betterAuth for email and password authentication with the cookie cache enabled for performance.
I use the useSession hook to react to auth changes in the navbar like so.

export default function Navbar() {
  const isMobile = useIsMobile();
  const user: User | null = authClient.useSession().data?.user as User | null;
  const links = user?.roleLevel ? authedLinks.slice(4 - user.roleLevel) : unauthedLinks;
  return (
    <header className="sticky top-0 right-0 left-0 z-50 flex justify-center">
      {isMobile ? (
        <MobileNavbar user={user} links={links} />
      ) : (
        <DesktopNavbar user={user} links={links} />
      )}
    </header>
  );
}


i use this to display the name and links to the pages they're allowed to access (with additional checks ofc). however an issue arises when the user changes their name, the name in the navbar doesn't change. the solution i've come up with is this, inside the code that handles the name change inside the event handler for the button that changes the name on the front end

        onSuccess={async (res) => {
          await authClient.getSession({ //pull fresh user info from the database
            query: {
              disableCookieCache: true,
            },
          });
          authClient.$store.notify("$sessionSignal"); //force the navbar to react
          toast.success("Updated successfully");
        }}


however i cannot find any documentation regarding manually pushing signals to the store so I'm not sure if this is necessarily correct
Was this page helpful?