fix add new withCorsMiddleware
parent
ca8ca913a8
commit
a4851bd0b6
|
@ -13,12 +13,11 @@ const today = new Date();
|
||||||
|
|
||||||
// This verifies the API key and sets the user if it is valid.
|
// This verifies the API key and sets the user if it is valid.
|
||||||
export const verifyApiKey: NextMiddleware = async (req, res, next) => {
|
export const verifyApiKey: NextMiddleware = async (req, res, next) => {
|
||||||
const pathIsDocs = req.url?.startsWith("/docs");
|
if (!req.query.apiKey) res.status(401).json({ message: "No API key provided" });
|
||||||
if (pathIsDocs) await next();
|
|
||||||
else 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 strippedApiKey = `${req.query.apiKey}`.replace(process.env.API_KEY_PREFIX || "cal_", "");
|
||||||
const hashedKey = hashAPIKey(strippedApiKey);
|
const hashedKey = hashAPIKey(strippedApiKey);
|
||||||
|
|
||||||
await prisma.apiKey
|
await prisma.apiKey
|
||||||
.findUnique({ where: { hashedKey } })
|
.findUnique({ where: { hashedKey } })
|
||||||
.then(async (apiKey) => {
|
.then(async (apiKey) => {
|
||||||
|
|
|
@ -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 };
|
|
@ -3,7 +3,7 @@ import pjson from "@/package.json";
|
||||||
import { withSwagger } from "next-swagger-doc";
|
import { withSwagger } from "next-swagger-doc";
|
||||||
|
|
||||||
import { withCors } from "@lib/helpers/withCors";
|
import { withCors } from "@lib/helpers/withCors";
|
||||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
import { withCorsMiddleware } from "@lib/helpers/withCorsMiddleware";
|
||||||
|
|
||||||
const swaggerHandler = withSwagger({
|
const swaggerHandler = withSwagger({
|
||||||
definition: {
|
definition: {
|
||||||
|
@ -19,4 +19,4 @@ const swaggerHandler = withSwagger({
|
||||||
tags: ["users", "teams", "memeberships"],
|
tags: ["users", "teams", "memeberships"],
|
||||||
sort: true,
|
sort: true,
|
||||||
});
|
});
|
||||||
export default withMiddleware(withCors)(swaggerHandler());
|
export default withCorsMiddleware()(swaggerHandler());
|
||||||
|
|
Loading…
Reference in New Issue