set cookies

This commit is contained in:
Nate Kelley 2025-01-23 17:01:52 -07:00
parent 02d0f9f2c4
commit 50b9177275
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
2 changed files with 15 additions and 14 deletions

View File

@ -1,21 +1,21 @@
import { createServerClient } from '@supabase/ssr'; import { CookieOptions, createServerClient } from '@supabase/ssr';
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
export async function createClient() { export const COOKIE_OPTIONS: CookieOptions = {
path: '/',
secure: process.env.NODE_ENV === 'production', // Only use secure in production
sameSite: 'lax', // Type assertion to fix the error
httpOnly: true, // Make cookies HttpOnly
maxAge: 60 * 60 * 24 * 7 // 1 week
};
export const createClient = async () => {
const cookieStore = await cookies(); const cookieStore = await cookies();
return await createServerClient( return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ {
cookieOptions: {
secure: true,
httpOnly: true
},
auth: {
autoRefreshToken: true,
persistSession: true
},
cookies: { cookies: {
getAll() { getAll() {
return cookieStore.getAll(); return cookieStore.getAll();
@ -23,7 +23,7 @@ export async function createClient() {
setAll(cookiesToSet) { setAll(cookiesToSet) {
try { try {
cookiesToSet.forEach(({ name, value, options }) => cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options) cookieStore.set(name, value, { ...options, ...COOKIE_OPTIONS })
); );
} catch { } catch {
// The `setAll` method was called from a Server Component. // The `setAll` method was called from a Server Component.
@ -34,4 +34,4 @@ export async function createClient() {
} }
} }
); );
} };

View File

@ -1,3 +1,4 @@
import { COOKIE_OPTIONS } from '@/context/Supabase/server';
import { createServerClient } from '@supabase/ssr'; import { createServerClient } from '@supabase/ssr';
import { User } from '@supabase/supabase-js'; import { User } from '@supabase/supabase-js';
import { NextResponse, type NextRequest } from 'next/server'; import { NextResponse, type NextRequest } from 'next/server';
@ -23,7 +24,7 @@ export async function updateSession(request: NextRequest) {
request request
}); });
cookiesToSet.forEach(({ name, value, options }) => cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options) supabaseResponse.cookies.set(name, value, { ...options, ...COOKIE_OPTIONS })
); );
} }
} }