cal.pub0.org/lib/helpers/verifyApiKey.ts

21 lines
790 B
TypeScript
Raw Normal View History

import { NextMiddleware } from "next-api-middleware";
// import { nanoid } from "nanoid";
import prisma from "@calcom/prisma";
const dateInPast = function (firstDate: Date, secondDate: Date) {
if (firstDate.setHours(0, 0, 0, 0) <= secondDate.setHours(0, 0, 0, 0)) {
return true;
}
}
const today = new Date();
export const verifyApiKey: NextMiddleware = async (req, res, next) => {
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.apiKey as string } });
if (!apiKey) {
res.status(400).json({ error: 'Your api key is not valid' });
throw new Error('No api key found');
}
if (apiKey.expiresAt && dateInPast(apiKey.expiresAt, today)) await next();
else res.status(400).json({ error: 'Your api key is not valid' });
};