import "server-only"; import { createServerClient } from "@supabase/ssr"; import { cookies } from "next/headers"; import type { Database } from "./types"; // For use in Server Components and Server Actions. Respects the signed-in // user's session, so all reads/writes go through RLS as that user. Uses only // the anon key (never the service role key) — the user's own session cookie // is what determines access, via RLS. export async function createClient() { const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; if (!supabaseUrl) { throw new Error("Missing NEXT_PUBLIC_SUPABASE_URL"); } if (!supabaseAnonKey) { throw new Error("Missing NEXT_PUBLIC_SUPABASE_ANON_KEY"); } const cookieStore = await cookies(); return createServerClient(supabaseUrl, supabaseAnonKey, { cookies: { getAll() { return cookieStore.getAll(); }, setAll(cookiesToSet) { try { cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options) ); } catch { // Called from a Server Component during render — safe to ignore // because proxy.ts refreshes the session cookie on every request. } }, }, }); }