Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
10 replies
Tribe

Adding Supabase to TRPC context

I am using Supabase for auth and db in a create t3 app project and I was curious if this would be the correct way to get the user session data into the trcp context.


I modified the createTRCPContext method to this...
server/api/trpc.ts
import { cookies } from 'next/headers'
import { createRouteHandlerClient  } from "@supabase/auth-helpers-nextjs"
import type * as schema from "../db/schema";

export const createTRPCContext = async (opts: { headers: Headers }) => {
  const cookieStore = cookies()

  const supabase = createRouteHandlerClient<typeof schema>({
    cookies: () => cookieStore,
  })

  const session = await supabase.auth.getSession()

  return {
    db,
    session,
    ...opts,
  };
};


or alternatively is it actually possible to just create a supabaseServerClient / createRouteHandlerClient within the TRCP procedures themselves whenever I need access to the session data?
Was this page helpful?