43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { createClient, type SupabaseClient } from "@supabase/supabase-js";
|
|
import { createLocalPostgresDataClient } from "@/lib/db/local-postgres-client-core";
|
|
import {
|
|
getSupabaseUrl,
|
|
SupabaseConfigurationError,
|
|
} from "./config";
|
|
|
|
export function createAdminSupabaseClient() {
|
|
if (process.env.AUTH_PROVIDER?.trim() === "self-hosted") {
|
|
const connectionString = process.env.SERVICE_DATABASE_URL?.trim();
|
|
if (!connectionString) {
|
|
throw new SupabaseConfigurationError(["SERVICE_DATABASE_URL"]);
|
|
}
|
|
if (!connectionString.startsWith("postgresql://")) {
|
|
throw new Error("SERVICE_DATABASE_URL must be a PostgreSQL URL");
|
|
}
|
|
return createLocalPostgresDataClient(
|
|
connectionString,
|
|
null,
|
|
"service_role",
|
|
) as unknown as SupabaseClient;
|
|
}
|
|
const url = getSupabaseUrl();
|
|
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
|
if (!serviceRoleKey) {
|
|
throw new SupabaseConfigurationError(["SUPABASE_SERVICE_ROLE_KEY"]);
|
|
}
|
|
|
|
return createClient(url, serviceRoleKey, {
|
|
auth: { autoRefreshToken: false, persistSession: false },
|
|
});
|
|
}
|
|
|
|
export function isAdminEmail(email: string | null | undefined) {
|
|
const configured = process.env.ADMIN_EMAILS;
|
|
if (!configured?.trim() || !email) return false;
|
|
|
|
const normalized = email.trim().toLowerCase();
|
|
return configured
|
|
.split(",")
|
|
.some((candidate) => candidate.trim().toLowerCase() === normalized);
|
|
}
|