First commit
This commit is contained in:
18
lib/supabase/browser.ts
Normal file
18
lib/supabase/browser.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { createClient, type SupabaseClient } from "@supabase/supabase-js";
|
||||
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!browserClient) {
|
||||
browserClient = createClient(url, anonKey);
|
||||
}
|
||||
|
||||
return browserClient;
|
||||
};
|
||||
49
lib/supabase/middleware.ts
Normal file
49
lib/supabase/middleware.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { createServerClient, type CookieOptions } from "@supabase/ssr";
|
||||
|
||||
export type SessionSnapshot = {
|
||||
response: NextResponse;
|
||||
isAuthed: boolean;
|
||||
userEmail: string | null;
|
||||
isConfigured: boolean;
|
||||
};
|
||||
|
||||
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) {
|
||||
return {
|
||||
response,
|
||||
isAuthed: false,
|
||||
userEmail: null,
|
||||
isConfigured: false,
|
||||
};
|
||||
}
|
||||
|
||||
const supabase = createServerClient(url, anonKey, {
|
||||
cookies: {
|
||||
getAll: () => req.cookies.getAll(),
|
||||
setAll: (
|
||||
cookiesToSet: Array<{
|
||||
name: string;
|
||||
value: string;
|
||||
options?: CookieOptions;
|
||||
}>,
|
||||
) => {
|
||||
cookiesToSet.forEach(({ name, value, options }) => {
|
||||
response.cookies.set(name, value, options);
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { data } = await supabase.auth.getUser();
|
||||
return {
|
||||
response,
|
||||
isAuthed: Boolean(data?.user),
|
||||
userEmail: data.user?.email ?? null,
|
||||
isConfigured: true,
|
||||
};
|
||||
};
|
||||
36
lib/supabase/server.ts
Normal file
36
lib/supabase/server.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { createServerClient, type CookieOptions } from "@supabase/ssr";
|
||||
|
||||
export const supabaseServer = async () => {
|
||||
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
||||
|
||||
if (!url || !anonKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cookieStore = await cookies();
|
||||
|
||||
return createServerClient(url, anonKey, {
|
||||
cookies: {
|
||||
getAll() {
|
||||
return cookieStore.getAll();
|
||||
},
|
||||
setAll(
|
||||
cookiesToSet: Array<{
|
||||
name: string;
|
||||
value: string;
|
||||
options?: CookieOptions;
|
||||
}>,
|
||||
) {
|
||||
try {
|
||||
cookiesToSet.forEach(({ name, value, options }) => {
|
||||
cookieStore.set(name, value, options);
|
||||
});
|
||||
} catch {
|
||||
// Server Components may not be able to write cookies.
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user