nextjs-scribase-starter/lib/supabase-server.ts
caelum0x 43875e1bb1 feat: SCR-DIR AGENTS.md for coding agents, claim flow and approver gate in README
AGENTS.md gives any coding agent the four steps: npm install,
npx scribase init --temp --yes --json, confirm first_request, create the
notes table from supabase/setup.sql, hand the human the claim link.
README gains the claim flow and the production approver gate.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-30 21:07:07 +03:00

45 lines
1.3 KiB
TypeScript

import { cookies } from "next/headers";
import { createServerClient, type CookieOptions } from "@supabase/ssr";
interface CookieToSet {
name: string;
value: string;
options: CookieOptions;
}
/**
* Server-side Supabase client, pointed at your Scribase project.
* `npx scribase init` writes NEXT_PUBLIC_SUPABASE_URL / NEXT_PUBLIC_SUPABASE_ANON_KEY
* into .env.local; the unprefixed names are read too, for other hosts.
*/
export function supabaseServer() {
const url = process.env.NEXT_PUBLIC_SUPABASE_URL ?? process.env.SUPABASE_URL;
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ?? process.env.SUPABASE_ANON_KEY;
if (!url || !anonKey) {
throw new Error(
"NEXT_PUBLIC_SUPABASE_URL / NEXT_PUBLIC_SUPABASE_ANON_KEY are not set. Run `npx scribase init --temp` first (see README)."
);
}
const cookieStore = cookies();
return createServerClient(url, anonKey, {
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet: CookieToSet[]) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options)
);
} catch {
// Called from a Server Component render; middleware refreshes the
// session instead. Safe to ignore here.
}
},
},
});
}