import { NextRequest, NextResponse } from 'next/server'; import { cookies } from 'next/headers'; const API_BASE_URL = process.env.API_BASE_URL || process.env.NEXT_PUBLIC_API_BASE_URL; export async function POST(request: NextRequest) { try { const body = await request.json(); // Forward to backend API const res = await fetch(`${API_BASE_URL}/api/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); if (!res.ok) { const text = await res.text().catch(() => ''); return NextResponse.json( { error: text || 'Login failed' }, { status: res.status } ); } const data = await res.json(); // Set HTTP-only cookie instead of returning token const cookieStore = await cookies(); cookieStore.set('session_token', data.token, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'lax', maxAge: 60 * 60 * 24 * 7, // 7 days path: '/', }); // Return user data without token return NextResponse.json({ user: data.user, }); } catch (err: any) { return NextResponse.json( { error: err?.message || 'Login failed' }, { status: 500 } ); } }