Refactor key access and role warning in Supabase clients
The update simplifies accessing environment keys and role warnings in Supabase clients by moving them to new functions 'getSupabaseClientKeys' and 'getServiceRoleKey'. The redundancy in the code is reduced promoting clearer and more maintainable code. The '@epic-web/invariant' import has been removed from files as it is no longer needed.
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
import { invariant } from '@epic-web/invariant';
|
||||
import { createBrowserClient } from '@supabase/ssr';
|
||||
|
||||
import { Database } from '../database.types';
|
||||
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||
|
||||
/**
|
||||
* @name getSupabaseBrowserClient
|
||||
* @description Get a Supabase client for use in the Browser
|
||||
*/
|
||||
export function getSupabaseBrowserClient<GenericSchema = Database>() {
|
||||
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const SUPABASE_ANON_KEY = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
||||
const keys = getSupabaseClientKeys();
|
||||
|
||||
invariant(SUPABASE_URL, `Supabase URL was not provided`);
|
||||
invariant(SUPABASE_ANON_KEY, `Supabase Anon key was not provided`);
|
||||
|
||||
return createBrowserClient<GenericSchema>(SUPABASE_URL, SUPABASE_ANON_KEY);
|
||||
return createBrowserClient<GenericSchema>(keys.url, keys.anonKey);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { CookieOptions } from '@supabase/ssr';
|
||||
import { createServerClient } from '@supabase/ssr';
|
||||
|
||||
import { Database } from '../database.types';
|
||||
import { getServiceRoleKey } from '../get-service-role-key';
|
||||
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||
|
||||
/**
|
||||
@@ -20,17 +21,7 @@ export function getSupabaseRouteHandlerClient<GenericSchema = Database>(
|
||||
const keys = getSupabaseClientKeys();
|
||||
|
||||
if (params.admin) {
|
||||
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.warn(
|
||||
`[Dev Only] You are using the Supabase Service Role. Make sure it's the right call.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!serviceRoleKey) {
|
||||
throw new Error('Supabase Service Role Key not provided');
|
||||
}
|
||||
const serviceRoleKey = getServiceRoleKey();
|
||||
|
||||
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
|
||||
auth: {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { cookies } from 'next/headers';
|
||||
import { createServerClient } from '@supabase/ssr';
|
||||
|
||||
import { Database } from '../database.types';
|
||||
import { getServiceRoleKey } from '../get-service-role-key';
|
||||
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||
|
||||
function createServerSupabaseClient() {
|
||||
@@ -22,17 +23,7 @@ export function getSupabaseServerActionClient<
|
||||
const admin = params?.admin ?? false;
|
||||
|
||||
if (admin) {
|
||||
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.warn(
|
||||
`[Dev Only] You are using the Supabase Service Role. Make sure it's the right call.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!serviceRoleKey) {
|
||||
throw new Error('Supabase Service Role Key not provided');
|
||||
}
|
||||
const serviceRoleKey = getServiceRoleKey();
|
||||
|
||||
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
|
||||
auth: {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { cookies } from 'next/headers';
|
||||
import { createServerClient } from '@supabase/ssr';
|
||||
|
||||
import { Database } from '../database.types';
|
||||
import { getServiceRoleKey } from '../get-service-role-key';
|
||||
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||
|
||||
/**
|
||||
@@ -19,17 +20,7 @@ export function getSupabaseServerComponentClient<GenericSchema = Database>(
|
||||
const keys = getSupabaseClientKeys();
|
||||
|
||||
if (params.admin) {
|
||||
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.warn(
|
||||
`[Dev Only] You are using the Supabase Service Role. Make sure it's the right call.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!serviceRoleKey) {
|
||||
throw new Error('Supabase Service Role Key not provided');
|
||||
}
|
||||
const serviceRoleKey = getServiceRoleKey();
|
||||
|
||||
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
|
||||
auth: {
|
||||
|
||||
Reference in New Issue
Block a user