forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase.ts
More file actions
65 lines (58 loc) · 2.61 KB
/
Copy pathsupabase.ts
File metadata and controls
65 lines (58 loc) · 2.61 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { createClient, SupabaseClient } from "@supabase/supabase-js";
import type { Database } from "./types";
/**
* Server-side Supabase client, used by every API route in src/app/api/.
*
* Uses the SERVICE ROLE key, which bypasses Row Level Security — that's
* intentional here: every API route already authorizes the request itself
* via the custom JWT session cookie (see lib/auth.ts) before touching the
* database, the same way the old Prisma setup did. This client must never
* be imported into a "use client" component — the service role key would
* end up shipped to the browser.
*
* The client is created lazily through a Proxy so that merely importing this
* module (which happens at build time for every route that uses it) never
* crashes when NEXT_PUBLIC_SUPABASE_URL / SUPABASE_SERVICE_ROLE_KEY are not
* set yet. The error only surfaces if an API route actually tries to touch
* the database without the env vars configured.
*
* The URL may be provided as NEXT_PUBLIC_SUPABASE_URL (standard Supabase
* naming, set in Vercel / Netlify) or SUPABASE_URL (used by this repo's
* local .env). Both are handled here so the app runs locally and on a host.
*/
const globalForSupabase = globalThis as unknown as {
supabase: SupabaseClient<Database> | undefined;
};
function getSupabaseClient(): SupabaseClient<Database> {
// Accept either naming convention — NEXT_PUBLIC_SUPABASE_URL (hosting
// convention) or SUPABASE_URL (this repo's local .env convention).
const supabaseUrl =
process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL;
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
if (!supabaseUrl || !serviceRoleKey) {
throw new Error(
"[supabase] NEXT_PUBLIC_SUPABASE_URL (or SUPABASE_URL) and SUPABASE_SERVICE_ROLE_KEY must both be set before calling the database. Add them to .env (local) or your host's project settings."
);
}
if (!globalForSupabase.supabase) {
globalForSupabase.supabase = createClient<Database>(supabaseUrl, serviceRoleKey, {
auth: { persistSession: false },
});
}
return globalForSupabase.supabase;
}
export const supabase = new Proxy(
{},
{
get(_target, prop) {
if (prop === "then") {
// Avoid the proxy being treated as a thenable by awaiting code.
return undefined;
}
const client = getSupabaseClient();
const value = (client as unknown as Record<PropertyKey, unknown>)[prop];
// Bind methods to the real client so `this` is correct when called.
return typeof value === "function" ? (value as Function).bind(client) : value;
},
}
) as SupabaseClient<Database>;