advance
This commit is contained in:
9
lib/supabase/browser.ts
Normal file → Executable file
9
lib/supabase/browser.ts
Normal file → Executable file
@@ -1,17 +1,16 @@
|
||||
import { createClient, type SupabaseClient } from "@supabase/supabase-js";
|
||||
import { readSupabasePublicConfig } from "@/lib/supabase/config";
|
||||
|
||||
let browserClient: SupabaseClient | null = null;
|
||||
|
||||
export const supabaseBrowser = (): SupabaseClient | null => {
|
||||
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
||||
|
||||
if (!url || !anonKey) {
|
||||
const config = readSupabasePublicConfig();
|
||||
if (!config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!browserClient) {
|
||||
browserClient = createClient(url, anonKey);
|
||||
browserClient = createClient(config.url, config.anonKey);
|
||||
}
|
||||
|
||||
return browserClient;
|
||||
|
||||
28
lib/supabase/config.ts
Normal file
28
lib/supabase/config.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
const isValidHttpUrl = (value: string): boolean => {
|
||||
try {
|
||||
const parsed = new URL(value);
|
||||
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export type SupabasePublicConfig = {
|
||||
url: string;
|
||||
anonKey: string;
|
||||
};
|
||||
|
||||
export const readSupabasePublicConfig = (): SupabasePublicConfig | null => {
|
||||
const url = process.env.NEXT_PUBLIC_SUPABASE_URL?.trim();
|
||||
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY?.trim();
|
||||
|
||||
if (!url || !anonKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isValidHttpUrl(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { url, anonKey };
|
||||
};
|
||||
8
lib/supabase/middleware.ts
Normal file → Executable file
8
lib/supabase/middleware.ts
Normal file → Executable file
@@ -1,5 +1,6 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { createServerClient, type CookieOptions } from "@supabase/ssr";
|
||||
import { readSupabasePublicConfig } from "@/lib/supabase/config";
|
||||
|
||||
export type SessionSnapshot = {
|
||||
response: NextResponse;
|
||||
@@ -11,9 +12,8 @@ export type SessionSnapshot = {
|
||||
export const updateSession = async (req: NextRequest): Promise<SessionSnapshot> => {
|
||||
const response = NextResponse.next({ request: req });
|
||||
|
||||
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
||||
if (!url || !anonKey) {
|
||||
const config = readSupabasePublicConfig();
|
||||
if (!config) {
|
||||
return {
|
||||
response,
|
||||
isAuthed: false,
|
||||
@@ -22,7 +22,7 @@ export const updateSession = async (req: NextRequest): Promise<SessionSnapshot>
|
||||
};
|
||||
}
|
||||
|
||||
const supabase = createServerClient(url, anonKey, {
|
||||
const supabase = createServerClient(config.url, config.anonKey, {
|
||||
cookies: {
|
||||
getAll: () => req.cookies.getAll(),
|
||||
setAll: (
|
||||
|
||||
9
lib/supabase/server.ts
Normal file → Executable file
9
lib/supabase/server.ts
Normal file → Executable file
@@ -1,17 +1,16 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { createServerClient, type CookieOptions } from "@supabase/ssr";
|
||||
import { readSupabasePublicConfig } from "@/lib/supabase/config";
|
||||
|
||||
export const supabaseServer = async () => {
|
||||
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
||||
|
||||
if (!url || !anonKey) {
|
||||
const config = readSupabasePublicConfig();
|
||||
if (!config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cookieStore = await cookies();
|
||||
|
||||
return createServerClient(url, anonKey, {
|
||||
return createServerClient(config.url, config.anonKey, {
|
||||
cookies: {
|
||||
getAll() {
|
||||
return cookieStore.getAll();
|
||||
|
||||
Reference in New Issue
Block a user