Nextjs - Refetch session every time page changes

How am i supposed to get the latest session data on every route change using useSession? it uses that same get-session throughout every route
4 Replies
lil rocket .30
lil rocket .30OP3mo ago
@Better Auth How am i supposed to get the latest session data on every route change using useSession? it uses that same get-session throughout every route
Better Auth
Better Auth3mo ago
When using useSession, it’s common to see that the same cached session is returned on every route change for performance reasons ([5]). By design, the hook uses caching (like cookie caching) so that you don’t always hit the backend on every route change, which speeds up your app ([21]). If you need the absolute latest session data (for example, after a mutation or to reflect real-time changes), you have a couple of options: 1. Use the refetch method that comes with useSession. Note that there’s an active discussion about allowing query parameters (such as { disableCookieCache: true }) with refetch for more fine-tuned control ([20]). In the meantime, you can explicitly call getSession with the disableCookieCache query option to force a fresh fetch and then update your UI accordingly. 2. Consider using the update function provided by useSession (as proposed in [25]) to mutate the session state manually after a change. These approaches help you balance between performance (with caching) and having the most current session data when needed. Sources: [5] Session Management | Better Auth [20] Add query parameter support to useSession().refetch() for cache control consistency [21] useSession on client return cached session instead of fetching [25] Mutate the session state when using the useSession() hook for JWT
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Session Management | Better Auth
Better Auth session management.
lil rocket .30
lil rocket .30OP3mo ago
@Better Auth can i change the name for /get-session
Better Auth
Better Auth3mo ago
Hi there! At the moment, Better Auth doesn’t offer a built-in option to rename the /get-session endpoint. This endpoint is a core part of the framework’s session management and is used consistently across plugins and client methods (see, for example, how it's used in the session management docs [2] and in the basic usage examples [6]). If you need further customization, you might consider extending or wrapping the functionality, but out-of-the-box renaming isn’t supported. Let me know if you have any more questions! Sources: [2] Session Management | Better Auth [6] Basic Usage | Better Auth
Session Management | Better Auth
Better Auth session management.
Basic Usage | Better Auth
Getting started with Better Auth

Did you find this page helpful?