Refactor Supabase client and service role key usage

The refactoring includes the moving of the `getSupabaseClientKeys()` and `getServiceRoleKey()` function calls to the module scope and the addition of a `warnServiceRoleKeyUsage()` function, used to display a warning when the Supabase Service Role is accessed. The change aims to improve code readability and maintainability.
This commit is contained in:
giancarlo
2024-04-13 20:03:08 +08:00
parent 4b9c023700
commit f0bc6959e1
4 changed files with 32 additions and 14 deletions

View File

@@ -5,12 +5,16 @@ import { cookies } from 'next/headers';
import { createServerClient } from '@supabase/ssr';
import { Database } from '../database.types';
import { getServiceRoleKey } from '../get-service-role-key';
import {
getServiceRoleKey,
warnServiceRoleKeyUsage,
} from '../get-service-role-key';
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
function createServerSupabaseClient() {
const keys = getSupabaseClientKeys();
const keys = getSupabaseClientKeys();
const serviceRoleKey = getServiceRoleKey();
function createServerSupabaseClient() {
return createServerClient<Database>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(),
});
@@ -23,7 +27,7 @@ export function getSupabaseServerActionClient<
const admin = params?.admin ?? false;
if (admin) {
const serviceRoleKey = getServiceRoleKey();
warnServiceRoleKeyUsage();
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {