This commit is contained in:
2025-10-08 22:37:14 +01:00
parent 00f1814f27
commit 566993b6e8
3 changed files with 31 additions and 3 deletions
+8
View File
@@ -35,6 +35,14 @@ export async function POST(request: NextRequest) {
role: user[0].role as 'user' | 'admin', role: user[0].role as 'user' | 'admin',
}); });
// Debug: Check if cookie was actually set
console.log('LOGIN: Session created for user:', user[0].email);
console.log('LOGIN: Request headers:', {
host: request.headers.get('host'),
'x-forwarded-proto': request.headers.get('x-forwarded-proto'),
'user-agent': request.headers.get('user-agent')
});
// Log the login activity // Log the login activity
await logActivity({ await logActivity({
userId: user[0].id, userId: user[0].id,
+12 -3
View File
@@ -73,13 +73,22 @@ export async function createSession(payload: Omit<SessionPayload, 'expiresAt'>)
const isSecure = process.env.NODE_ENV === 'production' && const isSecure = process.env.NODE_ENV === 'production' &&
process.env.NEXTAUTH_URL?.startsWith('https'); process.env.NEXTAUTH_URL?.startsWith('https');
cookieStore.set('session', session, { const cookieOptions = {
httpOnly: true, httpOnly: true,
secure: isSecure, secure: isSecure,
expires: expiresAt, expires: expiresAt,
sameSite: 'lax', sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
path: '/', path: '/',
} as const;
console.log('CREATE_SESSION: Setting cookie with options:', cookieOptions);
console.log('CREATE_SESSION: Environment:', {
NODE_ENV: process.env.NODE_ENV,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
isSecure
}); });
cookieStore.set('session', session, cookieOptions);
} }
export async function updateSession() { export async function updateSession() {
@@ -102,7 +111,7 @@ export async function updateSession() {
httpOnly: true, httpOnly: true,
secure: isSecure, secure: isSecure,
expires: expires, expires: expires,
sameSite: 'lax', sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
path: '/', path: '/',
}); });
} }
+11
View File
@@ -15,6 +15,17 @@ export default async function middleware(req: NextRequest) {
const isAuthRoute = authRoutes.includes(path); const isAuthRoute = authRoutes.includes(path);
const cookie = req.cookies.get('session')?.value; const cookie = req.cookies.get('session')?.value;
// Debug logging for production
if (!cookie && (isProtectedRoute || isAuthRoute)) {
console.log(`No session cookie found for ${path}, headers:`, {
host: req.headers.get('host'),
'x-forwarded-proto': req.headers.get('x-forwarded-proto'),
'x-forwarded-host': req.headers.get('x-forwarded-host'),
cookies: req.headers.get('cookie')
});
}
const session = await decrypt(cookie); const session = await decrypt(cookie);
// Redirect to login if accessing protected route without session // Redirect to login if accessing protected route without session