cal.pub0.org/pages/api/memberships/[id]/_auth-middleware.ts

18 lines
631 B
TypeScript
Raw Normal View History

import type { NextApiRequest } from "next";
import { HttpError } from "@calcom/lib/http-error";
2022-11-25 13:56:58 +00:00
import { membershipIdSchema } from "~/lib/validations/membership";
async function authMiddleware(req: NextApiRequest) {
const { userId, isAdmin, prisma } = req;
const { teamId } = membershipIdSchema.parse(req.query);
// Admins can just skip this check
if (isAdmin) return;
// Only team members can modify a membership
const membership = await prisma.membership.findFirst({ where: { userId, teamId } });
if (!membership) throw new HttpError({ statusCode: 403, message: "Forbidden" });
}
export default authMiddleware;