3

I've tried to check for session on server side in my NextJS project. I've used supabaseService client and also "createServerSupabaseClient" from @supabase/auth-helpers-nextjs both of those checks returns session null even tho I'm logged in in the client...

export async function getServerSideProps(
  context: GetServerSidePropsContext<{ tournamentId: string }>
) {
  // Create authenticated Supabase Client
  const supabaseServ = await supabaseService.auth.getSession();
  const supabase = await createServerSupabaseClient(context);
  // Check if we have a session
  const {
    data: { session },
  } = await supabase.auth.getSession();

  console.log("sessions", supabaseServ, session);
  
  if (!session)
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
}

Does anyone know why am I unable to getSession on SSR? Also I've console logged the cookies and it seems that I do have the JWT cookie in the request context...

Floky99
  • 562
  • 2
  • 8
  • 17
  • Where and how is `supabaseService` instantiated? What's the exact output of your console.log? Can you try removing the `supabaseService` line and make follow the example here? https://github.com/supabase/auth-helpers/blob/main/examples/nextjs/pages/profile.tsx#L22 – thorwebdev Jan 30 '23 at 05:07
  • 1) I assume supabaseService is something related to the `service_role` key - don't use it at all - it won't have your FE session 2) your code looks exactly like my code and should work: `createServerSupabaseClient` takes ctx and get new serverClient with correct FE user. So with this info, I can only confirm that you were doing it right. Let us know what was wrong :) – andriishupta Feb 01 '23 at 17:03

0 Answers0