fix for cf compatibility

This commit is contained in:
2025-10-08 22:49:20 +01:00
parent 566993b6e8
commit 72410be343
+6 -10
View File
@@ -67,17 +67,15 @@ export async function createSession(payload: Omit<SessionPayload, 'expiresAt'>)
const cookieStore = await cookies(); const cookieStore = await cookies();
// In production, always use secure cookies if NEXTAUTH_URL is https // For Cloudflare tunnel: external is HTTPS, internal is HTTP
// This handles Cloudflare tunnel scenarios where external URL is https // Use secure cookies when NEXTAUTH_URL is https (external URL)
// but internal communication is http const isSecure = process.env.NEXTAUTH_URL?.startsWith('https') ?? false;
const isSecure = process.env.NODE_ENV === 'production' &&
process.env.NEXTAUTH_URL?.startsWith('https');
const cookieOptions = { const cookieOptions = {
httpOnly: true, httpOnly: true,
secure: isSecure, secure: isSecure,
expires: expiresAt, expires: expiresAt,
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax', sameSite: isSecure ? 'none' : 'lax', // none required for secure cross-site
path: '/', path: '/',
} as const; } as const;
@@ -103,15 +101,13 @@ export async function updateSession() {
const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000); const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
const newSession = await encrypt({ ...payload, expiresAt: expires }); const newSession = await encrypt({ ...payload, expiresAt: expires });
// In production, always use secure cookies if NEXTAUTH_URL is https const isSecure = process.env.NEXTAUTH_URL?.startsWith('https') ?? false;
const isSecure = process.env.NODE_ENV === 'production' &&
process.env.NEXTAUTH_URL?.startsWith('https');
cookieStore.set('session', newSession, { cookieStore.set('session', newSession, {
httpOnly: true, httpOnly: true,
secure: isSecure, secure: isSecure,
expires: expires, expires: expires,
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax', sameSite: isSecure ? 'none' : 'lax',
path: '/', path: '/',
}); });
} }