import type { APIRoute } from 'astro';
import { isValidLogin, setAdminSession } from '../../../lib/adminAuth';

export const POST: APIRoute = async (context) => {
  const formData = await context.request.formData();
  const username = String(formData.get('username') ?? '');
  const password = String(formData.get('password') ?? '');

  if (!isValidLogin(username, password)) {
    return context.redirect('/admin/login/?error=1', 302);
  }

  setAdminSession(context);

  return context.redirect('/admin/', 302);
};
