Files
ftdl/app/api/login/route.ts
tech 46d9f3a299 fix: set cookie secure flag based on request protocol
Fix login issue when accessing via IP address over HTTP. Previously,
the cookie secure flag was set based on NODE_ENV which caused
cookies to not be sent when accessing via HTTP in production mode.
Now it uses the actual request protocol (checking x-forwarded-proto
header or request URL).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-03-04 13:03:44 +08:00

41 lines
1.1 KiB
TypeScript

// app/api/login/route.ts
import { NextResponse } from 'next/server';
import { AUTH_USERNAME, AUTH_PASSWORD, COOKIE_NAME, COOKIE_MAX_AGE } from '@/lib/constants';
export async function POST(request: Request) {
try {
const body = await request.json();
const { username, password } = body;
if (!username || !password) {
return NextResponse.json(
{ error: 'Username and password are required' },
{ status: 400 }
);
}
if (username === AUTH_USERNAME && password === AUTH_PASSWORD) {
const response = NextResponse.json({ success: true });
const isHttps = request.headers.get('x-forwarded-proto') === 'https' || request.url.startsWith('https:');
response.cookies.set(COOKIE_NAME, 'ok', {
httpOnly: true,
secure: isHttps,
sameSite: 'lax',
maxAge: COOKIE_MAX_AGE,
path: '/',
});
return response;
}
return NextResponse.json(
{ error: 'Invalid credentials' },
{ status: 401 }
);
} catch (error) {
return NextResponse.json(
{ error: 'Invalid request' },
{ status: 400 }
);
}
}