Refactored Supabase Clients using the new recommended approach by Supabase by centralizing all clients around one single implementation. (#51)
The previous clients have been marked as deprecated and will be removed at some point.
This commit is contained in:
committed by
GitHub
parent
2f0c4b4ae3
commit
ba6e649461
57
packages/supabase/src/clients/server-component-client.ts
Normal file
57
packages/supabase/src/clients/server-component-client.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'server-only';
|
||||
|
||||
import { unstable_noStore as noStore } from 'next/cache';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
import { createServerClient } from '@supabase/ssr';
|
||||
|
||||
import { Database } from '../database.types';
|
||||
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
|
||||
*/
|
||||
export function getSupabaseServerComponentClient<GenericSchema = Database>(
|
||||
params = {
|
||||
admin: false,
|
||||
},
|
||||
) {
|
||||
// prevent any caching (to be removed in Next v15)
|
||||
noStore();
|
||||
|
||||
if (params.admin) {
|
||||
warnServiceRoleKeyUsage();
|
||||
|
||||
return createClient<GenericSchema>(keys.url, serviceRoleKey, {
|
||||
auth: {
|
||||
persistSession: false,
|
||||
autoRefreshToken: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
|
||||
cookies: getCookiesStrategy(),
|
||||
});
|
||||
}
|
||||
|
||||
function getCookiesStrategy() {
|
||||
const cookieStore = cookies();
|
||||
|
||||
return {
|
||||
get: (name: string) => {
|
||||
return cookieStore.get(name)?.value;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user