Trouble with understanding useSession function from @tanstack/react-start-server
I am trying to implement auth with tanstack-start with react query. I followed basic auth starter example from the docs but I am getting this error:
When I log current session and userId, they are logged currently (inside getCurrentUserId function)
Current setup looks like:
Any help would be really appreciated. Thanks!
5 Replies
quickest-silverOP•5mo ago
@Tanner Linsley
afraid-scarlet•5mo ago
If you check the source code you’ll see is a wrapper over https://h3.unjs.io/examples/handle-session
https://github.com/TanStack/router/blob/main/packages/start-server-core/src/h3.ts#L396
Handle Session - h3
Remember your users using a session.
GitHub
router/packages/start-server-core/src/h3.ts at main · TanStack/router
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - TanStack/router
quickest-silverOP•5mo ago
@Jaga Santagostino
So it means, tanstack start should handle this? Or am I missing something? In their basic-auth example, session is implemented in the same way I did. Docs doesn't mention anything about this.
Do you have any idea what might be the issue and possible ways to resolve it?
protestant-coral•5mo ago
I think you're not supposed to use this in the client. It uses cookies to manage the session so it needs to be called server-side.
Here's how I do it in my app:
Then I use it in my __root beforeLoad to add it to the global context
Then later in your app you can get it in your client code with the route context:
quickest-silverOP•5mo ago
thank you so much @Simon
I really appreciate it.