2022-03-28 14:05:50 +00:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
2022-03-28 00:51:40 +00:00
|
|
|
// 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,
|
2022-03-28 00:51:40 +00:00
|
|
|
// but we could also check if the apiKey is valid if we had prisma here.
|
2022-03-30 12:17:55 +00:00
|
|
|
|
2022-03-30 15:37:51 +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 00:51:40 +00:00
|
|
|
|
2022-03-28 14:05:50 +00:00
|
|
|
if (apiKey) return response;
|
2022-03-31 20:14:37 +00:00
|
|
|
// if no apiKey is passed, we throw early a 401 unauthorized asking for a valid apiKey
|
2022-03-28 14:05:50 +00:00
|
|
|
else
|
2022-03-30 14:56:24 +00:00
|
|
|
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
|
|
|
);
|
2022-03-28 00:51:40 +00:00
|
|
|
}
|