2022-10-21 19:54:28 +00:00
|
|
|
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";
|
2022-10-21 19:54:28 +00:00
|
|
|
|
|
|
|
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;
|