18 lines
515 B
TypeScript
18 lines
515 B
TypeScript
|
|
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;
|
||
|
|
}
|
||
|
|
}
|