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';
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();
return await createServerClient(
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookieOptions: {
secure: true,
httpOnly: true
},
auth: {
autoRefreshToken: true,
persistSession: true
},
cookies: {
getAll() {
return cookieStore.getAll();
@ -23,7 +23,7 @@ export async function createClient() {
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options)
cookieStore.set(name, value, { ...options, ...COOKIE_OPTIONS })
);
} catch {
// 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 { User } from '@supabase/supabase-js';
import { NextResponse, type NextRequest } from 'next/server';
@ -23,7 +24,7 @@ export async function updateSession(request: NextRequest) {
request
});
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
supabaseResponse.cookies.set(name, value, { ...options, ...COOKIE_OPTIONS })
);
}
}