Merge pull request #39 from calcom/fix/middleware

This commit is contained in:
Agusti Fernandez
2022-04-15 17:05:30 +02:00
committed by GitHub
3 changed files with 16 additions and 5 deletions
+2 -3
View File
@@ -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) => {
+12
View File
@@ -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 };
+2 -2
View File
@@ -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());