WFDASH-WEBAPP/src/lib/server/pos-auth.ts
Dave af7a74b4fa feat(pos): add assertPosAuth for POS_API_KEY-gated routes
Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-31 01:30:52 +02:00

22 lines
723 B
TypeScript

import { NextRequest } from "next/server";
/**
* Device-scoped secret, distinct from AGENT_API_KEY — a POS device
* only needs checkout/catalog/sales access, not the full agent API
* surface, so it gets its own key rather than reusing the agent's.
*/
export function assertPosAuth(req: NextRequest) {
const expected = process.env.POS_API_KEY;
if (!expected) {
throw new Error("POS_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;
}
}