TKK_E32230469/web_ups/src/app/api/auth/login/route.ts

40 lines
1.1 KiB
TypeScript

import { NextResponse } from 'next/server';
import { encrypt } from '@/lib/auth';
import { cookies } from 'next/headers';
export async function POST(request: Request) {
try {
const { username, password } = await request.json();
const validUsername = process.env.DASHBOARD_USERNAME || 'admin';
const validPassword = process.env.DASHBOARD_PASSWORD || 'admin';
if (username === validUsername && password === validPassword) {
// Create session
const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000); // 7 days
const session = await encrypt({ username, expires });
// Save the session in a cookie
(await cookies()).set('auth_token', session, {
expires,
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
path: '/',
});
return NextResponse.json({ success: true });
}
return NextResponse.json(
{ error: 'Invalid username or password' },
{ status: 401 }
);
} catch (error) {
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}