cal.pub0.org/pages/api/_middleware.ts

22 lines
804 B
TypeScript
Raw Normal View History

2022-03-28 14:05:50 +00:00
import { NextRequest, NextResponse } from "next/server";
// Not much useful yet as prisma.client can't be used in the middlewares (client is not available)
2022-03-28 14:05:50 +00:00
// For now we just throw early if no apiKey is passed,
// but we could also check if the apiKey is valid if we had prisma here.
2022-03-30 12:17:55 +00:00
export default async function requireApiKeyAsQueryParams({ nextUrl }: NextRequest) {
2022-03-28 14:05:50 +00:00
const response = NextResponse.next();
const apiKey = nextUrl.searchParams.get("apiKey");
2022-03-28 14:05:50 +00:00
if (apiKey) return response;
// if no apiKey is passed, we throw early a 401 unauthorized
2022-03-28 14:05:50 +00:00
else
new NextResponse(
JSON.stringify({
message:
"You need to pass an apiKey as query param: https://api.cal.com/resource?apiKey=<your-api-key>",
}),
{ status: 401, statusText: "Unauthorized" }
2022-03-28 14:05:50 +00:00
);
}