Skip to content

Scheduling infrastructure for absolutely everyone.

fix add new withCorsMiddleware

Agusti Fernandez Pardoa4851bd0b69e
diff --git a/lib/helpers/verifyApiKey.ts b/lib/helpers/verifyApiKey.ts
index 9a40ce5f25e..6fe33c6ad3e 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 00000000000..14182eff996
--- /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 39b36362f52..e4f721bb8f0 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());