From a4851bd0b69e6091b14830a9b745f0a28cb5a142 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Fri, 15 Apr 2022 17:04:17 +0200 Subject: [PATCH] fix add new withCorsMiddleware --- lib/helpers/verifyApiKey.ts | 5 ++--- lib/helpers/withCorsMiddleware.ts | 12 ++++++++++++ pages/api/docs.ts | 4 ++-- 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 lib/helpers/withCorsMiddleware.ts diff --git a/lib/helpers/verifyApiKey.ts b/lib/helpers/verifyApiKey.ts index 9a40ce5f25..6fe33c6ad3 100644 --- a/lib/helpers/verifyApiKey.ts +++ b/lib/helpers/verifyApiKey.ts @@ -13,12 +13,11 @@ const today = new Date(); // This verifies the API key and sets the user if it is valid. export const verifyApiKey: NextMiddleware = async (req, res, next) => { - const pathIsDocs = req.url?.startsWith("/docs"); - if (pathIsDocs) await next(); - else if (!req.query.apiKey) res.status(401).json({ message: "No API key provided" }); + if (!req.query.apiKey) res.status(401).json({ message: "No API key provided" }); const strippedApiKey = `${req.query.apiKey}`.replace(process.env.API_KEY_PREFIX || "cal_", ""); const hashedKey = hashAPIKey(strippedApiKey); + await prisma.apiKey .findUnique({ where: { hashedKey } }) .then(async (apiKey) => { diff --git a/lib/helpers/withCorsMiddleware.ts b/lib/helpers/withCorsMiddleware.ts new file mode 100644 index 0000000000..14182eff99 --- /dev/null +++ b/lib/helpers/withCorsMiddleware.ts @@ -0,0 +1,12 @@ +import { label } from "next-api-middleware"; + +import { withCors } from "./withCors"; + +const withCorsMiddleware = label( + { + withCors, + }, + ["withCors"] // <-- Provide a list of middleware to call automatically +); + +export { withCorsMiddleware }; diff --git a/pages/api/docs.ts b/pages/api/docs.ts index 39b36362f5..e4f721bb8f 100644 --- a/pages/api/docs.ts +++ b/pages/api/docs.ts @@ -3,7 +3,7 @@ import pjson from "@/package.json"; import { withSwagger } from "next-swagger-doc"; import { withCors } from "@lib/helpers/withCors"; -import { withMiddleware } from "@lib/helpers/withMiddleware"; +import { withCorsMiddleware } from "@lib/helpers/withCorsMiddleware"; const swaggerHandler = withSwagger({ definition: { @@ -19,4 +19,4 @@ const swaggerHandler = withSwagger({ tags: ["users", "teams", "memeberships"], sort: true, }); -export default withMiddleware(withCors)(swaggerHandler()); +export default withCorsMiddleware()(swaggerHandler());