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

@@ -6,9 +6,15 @@ import type { CookieOptions } from '@supabase/ssr';
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';
const serviceRoleKey = getServiceRoleKey();
const keys = getSupabaseClientKeys();
/**
* @name getSupabaseRouteHandlerClient
* @description Get a Supabase client for use in the Route Handler Routes
@@ -18,10 +24,8 @@ export function getSupabaseRouteHandlerClient<GenericSchema = Database>(
admin: false,
},
) {
const keys = getSupabaseClientKeys();
if (params.admin) {
const serviceRoleKey = getServiceRoleKey();
warnServiceRoleKeyUsage();
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {

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: {

View File

@@ -5,9 +5,15 @@ 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';
const serviceRoleKey = getServiceRoleKey();
const keys = getSupabaseClientKeys();
/**
* @name getSupabaseServerComponentClient
* @description Get a Supabase client for use in the Server Components
@@ -17,10 +23,8 @@ export function getSupabaseServerComponentClient<GenericSchema = Database>(
admin: false,
},
) {
const keys = getSupabaseClientKeys();
if (params.admin) {
const serviceRoleKey = getServiceRoleKey();
warnServiceRoleKeyUsage();
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {

View File

@@ -1,3 +1,5 @@
import 'server-only';
import { z } from 'zod';
/**
@@ -7,12 +9,16 @@ import { z } from 'zod';
*/
export function getServiceRoleKey() {
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
return z.string().min(1).parse(serviceRoleKey);
}
/**
* Displays a warning message if the Supabase Service Role is being used.
*/
export function warnServiceRoleKeyUsage() {
if (process.env.NODE_ENV !== 'production') {
console.warn(
`[Dev Only] This is a simple warning to let you know you are using the Supabase Service Role. Make sure it's the right call.`,
);
}
return z.string().min(1).parse(serviceRoleKey);
}