-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-role.ts
More file actions
36 lines (31 loc) · 1.09 KB
/
Copy pathservice-role.ts
File metadata and controls
36 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import "server-only";
import { createClient } from "@supabase/supabase-js";
/**
* Server-only Supabase client backed by the service role key.
*
* Use this when an API route needs to write rows on behalf of an anonymous
* caller (e.g. the public event submissions form). The service role bypasses
* RLS entirely, so:
*
* - NEVER import this from client code or surface its output to the browser.
* - The calling route is responsible for validating input and authorization.
*
* Wrapped in a factory so we throw early at first use if the env var is
* missing, rather than failing silently inside a query.
*/
export function createServiceRoleClient() {
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
if (!url) {
throw new Error("Missing required environment variable: NEXT_PUBLIC_SUPABASE_URL");
}
if (!serviceRoleKey) {
throw new Error("Missing required environment variable: SUPABASE_SERVICE_ROLE_KEY");
}
return createClient(url, serviceRoleKey, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
});
}