25 lines
728 B
TypeScript
25 lines
728 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { validateApiKey } from "@/lib/auth/api-key";
|
|
|
|
/**
|
|
* Authenticate an API request with Bearer token
|
|
* Returns the claw object if authenticated, or a 401 NextResponse if not
|
|
*/
|
|
export async function authenticateRequest(req: NextRequest) {
|
|
const authHeader = req.headers.get("authorization");
|
|
if (!authHeader?.startsWith("Bearer ")) {
|
|
return NextResponse.json(
|
|
{ error: "Missing or invalid authorization header" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const apiKey = authHeader.slice(7);
|
|
const claw = await validateApiKey(apiKey);
|
|
if (!claw) {
|
|
return NextResponse.json({ error: "Invalid API key" }, { status: 401 });
|
|
}
|
|
|
|
return { claw };
|
|
}
|