WFDASH-WEBAPP/src/lib/server/agent-auth.ts

18 lines
516 B
TypeScript
Raw Normal View History

import { NextRequest } from "next/server";
export function assertAgentAuth(req: NextRequest) {
const expected = process.env.AGENT_API_KEY;
if (!expected) {
throw new Error("AGENT_API_KEY is not configured.");
}
const authHeader = req.headers.get("authorization") || "";
const token = authHeader.startsWith("Bearer ") ? authHeader.slice(7) : "";
if (token !== expected) {
const error = new Error("Unauthorized");
(error as Error & { status?: number }).status = 401;
throw error;
}
}