File size: 1,035 Bytes
101ebaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { NextRequest, NextResponse } from "next/server";
import { adminCookieOptions, createAdminSessionValue } from "@/lib/adminAuth";

export async function POST(request: NextRequest) {
    try {
        const adminPassword = process.env.ADMIN_PASSWORD || "";
        if (!adminPassword) {
            return NextResponse.json({ error: "ADMIN_PASSWORD is not set" }, { status: 500 });
        }

        const body = await request.json();
        const password = typeof body?.password === "string" ? body.password : "";

        if (password !== adminPassword) {
            return NextResponse.json({ error: "Invalid password" }, { status: 401 });
        }

        const response = NextResponse.json({ ok: true });
        response.cookies.set({
            ...adminCookieOptions(),
            value: createAdminSessionValue(),
        });
        return response;
    } catch (error) {
        console.error("Admin login error:", error);
        return NextResponse.json({ error: "Failed to login" }, { status: 500 });
    }
}