Trouble with understanding useSession
function from @tanstack/react-start-server
#4016
Replies: 1 comment
-
Answered on Discord as well, I think you're not supposed to use this in the client. Here's how I do it in my app: import { createServerFn } from "@tanstack/react-start";
import { useAppSession } from "../session";
export const fetchSession = createServerFn({ method: "GET" }).handler(
async () => {
// We need to auth on the server so we have access to secure cookies
const session = await useAppSession();
return session.data;
},
); Then I use it in my __root beforeLoad to add it to the global context export const Route = createRootRouteWithContext<AppContext>()({
beforeLoad: async ({ location }) => {
const [session] = await Promise.all([
fetchSession(),
]);
return { session };
}
}); Then later in your app you can get it in your client code with the route context: const { session } = Route.useRouteContext(); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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!
Beta Was this translation helpful? Give feedback.
All reactions