44 lines
1.1 KiB
TypeScript
44 lines
1.1 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 SUPABASE_URL / SUPABASE_ANON_KEY into .env.
|
|
*/
|
|
export function supabaseServer() {
|
|
const url = process.env.SUPABASE_URL;
|
|
const anonKey = process.env.SUPABASE_ANON_KEY;
|
|
|
|
if (!url || !anonKey) {
|
|
throw new Error(
|
|
"SUPABASE_URL / SUPABASE_ANON_KEY are not set. Run `npx scribase init` 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.
|
|
}
|
|
},
|
|
},
|
|
});
|
|
}
|