This commit is contained in:
giancarlo
2024-03-24 02:23:22 +08:00
parent 648d77b430
commit bce3479368
589 changed files with 37067 additions and 9596 deletions

View File

@@ -0,0 +1,20 @@
import { invariant } from '@epic-web/invariant';
import { createBrowserClient } from '@supabase/ssr';
import { Database } from '../database.types';
let client: ReturnType<typeof createBrowserClient>;
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;
invariant(SUPABASE_URL, `Supabase URL was not provided`);
invariant(SUPABASE_ANON_KEY, `Supabase Anon key was not provided`);
if (client) return client;
client = createBrowserClient<GenericSchema>(SUPABASE_URL, SUPABASE_ANON_KEY);
return client;
}

View File

@@ -0,0 +1,65 @@
import { type NextRequest, NextResponse } from 'next/server';
import { type CookieOptions, createServerClient } from '@supabase/ssr';
import { Database } from '../database.types';
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
/**
* Creates a middleware client for Supabase.
*
* @param {NextRequest} request - The Next.js request object.
* @param {NextResponse} response - The Next.js response object.
*/
export function createMiddlewareClient<GenericSchema = Database>(
request: NextRequest,
response: NextResponse,
) {
const keys = getSupabaseClientKeys();
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
cookies: getCookieStrategy(request, response),
});
}
function getCookieStrategy(request: NextRequest, response: NextResponse) {
return {
set: (name: string, value: string, options: CookieOptions) => {
request.cookies.set({ name, value, ...options });
response = NextResponse.next({
request: {
headers: request.headers,
},
});
response.cookies.set({
name,
value,
...options,
});
},
get: (name: string) => {
return request.cookies.get(name)?.value;
},
remove: (name: string, options: CookieOptions) => {
request.cookies.set({
name,
value: '',
...options,
});
response = NextResponse.next({
request: {
headers: request.headers,
},
});
response.cookies.set({
name,
value: '',
...options,
});
},
};
}

View File

@@ -0,0 +1,61 @@
import { cookies } from 'next/headers';
import type { CookieOptions } from '@supabase/ssr';
import { createServerClient } from '@supabase/ssr';
import 'server-only';
import { Database } from '../database.types';
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
/**
* @name getSupabaseRouteHandlerClient
* @description Get a Supabase client for use in the Route Handler Routes
*/
export function getSupabaseRouteHandlerClient<GenericSchema = Database>(
params = {
admin: false,
},
) {
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');
}
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {
persistSession: false,
},
cookies: {},
});
}
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(),
});
}
function getCookiesStrategy() {
const cookieStore = cookies();
return {
set: (name: string, value: string, options: CookieOptions) => {
cookieStore.set({ name, value, ...options });
},
get: (name: string) => {
return cookieStore.get(name)?.value;
},
remove: (name: string, options: CookieOptions) => {
cookieStore.set({ name, value: '', ...options });
},
};
}

View File

@@ -0,0 +1,67 @@
import { cookies } from 'next/headers';
import { createServerClient } from '@supabase/ssr';
import 'server-only';
import { Database } from '../database.types';
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
const createServerSupabaseClient = <GenericSchema = Database>() => {
const keys = getSupabaseClientKeys();
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(),
});
};
export const getSupabaseServerActionClient = <
GenericSchema = Database,
>(params?: {
admin: false;
}) => {
const keys = getSupabaseClientKeys();
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');
}
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {
persistSession: false,
},
cookies: {},
});
}
return createServerSupabaseClient<GenericSchema>();
};
function getCookiesStrategy() {
const cookieStore = cookies();
return {
get: (name: string) => {
return cookieStore.get(name)?.value;
},
set: (name: string, value: string, options: object) => {
cookieStore.set({ name, value, ...options });
},
remove: (name: string, options: object) => {
cookieStore.set({
name,
value: '',
...options,
});
},
};
}

View File

@@ -0,0 +1,54 @@
import { cookies } from 'next/headers';
import { createServerClient } from '@supabase/ssr';
import 'server-only';
import { Database } from '../database.types';
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
/**
* @name getSupabaseServerComponentClient
* @description Get a Supabase client for use in the Server Components
*/
export const getSupabaseServerComponentClient = <GenericSchema = Database>(
params = {
admin: false,
},
) => {
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');
}
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {
persistSession: false,
},
cookies: {},
});
}
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(),
});
};
function getCookiesStrategy() {
const cookieStore = cookies();
return {
get: (name: string) => {
return cookieStore.get(name)?.value;
},
};
}