TanStackT
TanStack6mo ago
34 replies
yucky-gold

cookie issue in production

I'm using the below code after setting up ssr in my root page so i thought of validating the session of the user in the server side only, I'm using the following code below for the session fetching which I am calling from beforeload function of __root.tsx file, it works totally fine on localhost but when i pushed it to prod it doesn't work, I guess the issue happened as both backend and frontend are deployed on different addresses. Before ssr everything works fine . Here's the sample of my code.
import { createServerFn } from "@tanstack/react-start";
import { getHeaders } from "@tanstack/react-start/server";
import { authClient } from "./auth";
import { client } from "./client";

export const getSessionServer = createServerFn({
  method: 'GET'
})
  .validator(() => ({}))
  .handler(async () => {
    try {
      const headers: any = getHeaders();
      console.log("headers", headers);
      const session = await authClient.getSession({
        fetchOptions: {
          headers,
          credentials: 'include'
        }
      });

      console.log("session=======>", session);
      
      return {
        user: session?.data?.user || null,
        session: session?.data?.session || null,
      };
    } catch (error) {
      console.error('Failed to get session:', error);
      return {
        user: null,
        session: null,
      };
    }
  });

Am i missing anything ?
Was this page helpful?