Plug on/off

This commit is contained in:
Jack Mechem 2026-05-21 19:17:40 -07:00
parent 130bed1d1f
commit 876da0c384
Signed by: jackmechem
SSH key fingerprint: SHA256:GjIjMAC33pzYOe+hWcX5uvgnPrVFAXSrquvt84AOJbU
4 changed files with 100 additions and 26 deletions

View file

@ -0,0 +1,25 @@
import { NextRequest, NextResponse } from "next/server";
const ALLOWED_ACTIONS = ["on", "off"];
export async function POST(
req: NextRequest,
{ params }: { params: Promise<{ device: string; action: string }> },
) {
const token = req.cookies.get("token")?.value;
if (!token) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { device, action } = await params;
if (!ALLOWED_ACTIONS.includes(action)) {
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
}
const res = await fetch(`http://localhost:3001/power/${device}/${action}`, {
method: "POST",
});
return new NextResponse(null, { status: res.status });
}