Files
mnote/wolai-frontend/src/lib/supabase/server.ts
T

37 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-11-23 10:55:04 +08:00
import { createServerComponentClient, createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
import { getDecodedCookies } from "@/lib/server-cookies";
export const createSupabaseServerClient = async () => {
const cookieStore = await getDecodedCookies();
2026-01-10 23:08:56 +08:00
const supabaseUrl = process.env.SUPABASE_INTERNAL_URL ?? process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY ?? process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseAnonKey) {
throw new Error(
"缺少 Supabase 环境变量,请配置 NEXT_PUBLIC_SUPABASE_URL / NEXT_PUBLIC_SUPABASE_ANON_KEY(可选:SUPABASE_INTERNAL_URL / SUPABASE_ANON_KEY 作为服务端内网地址)",
);
}
2025-11-23 10:55:04 +08:00
return createServerComponentClient({
2026-01-08 06:28:14 +08:00
cookies: () => cookieStore as any,
2026-01-10 23:08:56 +08:00
supabaseUrl,
supabaseKey: supabaseAnonKey,
2026-01-08 06:28:14 +08:00
} as any);
2025-11-23 10:55:04 +08:00
};
export const createSupabaseRouteClient = async () => {
const cookieStore = await getDecodedCookies();
2026-01-10 23:08:56 +08:00
const supabaseUrl = process.env.SUPABASE_INTERNAL_URL ?? process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY ?? process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseAnonKey) {
throw new Error(
"缺少 Supabase 环境变量,请配置 NEXT_PUBLIC_SUPABASE_URL / NEXT_PUBLIC_SUPABASE_ANON_KEY(可选:SUPABASE_INTERNAL_URL / SUPABASE_ANON_KEY 作为服务端内网地址)",
);
}
2025-11-23 10:55:04 +08:00
return createRouteHandlerClient({
2026-01-08 06:28:14 +08:00
cookies: () => cookieStore as any,
2026-01-10 23:08:56 +08:00
supabaseUrl,
supabaseKey: supabaseAnonKey,
2026-01-08 06:28:14 +08:00
} as any);
2025-11-23 10:55:04 +08:00
};