Skip to content

Commit

Permalink
Add Protection on Organiser API Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
sampoder committed Oct 24, 2023
1 parent 8ea00ba commit f503e61
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 38 deletions.
5 changes: 3 additions & 2 deletions pages/api/organizer/hackathons/[slug]/check-in/[attendee].ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
import { getHackathon } from "..";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });

const { attendee, slug } = req.query;

Expand Down
5 changes: 3 additions & 2 deletions pages/api/organizer/hackathons/[slug]/data/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import type { Attendee } from "@prisma/client";
import { NextApiRequest, NextApiResponse } from "next";
import { getHackathon } from "..";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });
let attendee: Attendee = await prisma.attendee.create({
data: {
email: req.body.email,
Expand Down
5 changes: 3 additions & 2 deletions pages/api/organizer/hackathons/[slug]/data/delete.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
import { getHackathon } from "..";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });

const { ids } = req.body;

Expand Down
14 changes: 3 additions & 11 deletions pages/api/organizer/hackathons/[slug]/data/form.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";

import { getHackathon } from "..";
import type { AttendeeAttribute, Hackathon } from "@prisma/client";

type HackathonWithAttributes = Hackathon & {
Expand All @@ -12,16 +12,8 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
let hackathon = (await prisma.hackathon.findUnique({
where: {
slug: req.query.slug as string
},
include: {
attendeeAttributes: true
}
})) as HackathonWithAttributes;
const hackathon = await getHackathon(req, res, {attendeeAttributes: true}) as HackathonWithAttributes | null
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });
let newData: any = {};
Object.keys(req.body).map((x) => {
let id = x.split("_")[0];
Expand Down
13 changes: 4 additions & 9 deletions pages/api/organizer/hackathons/[slug]/data/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getAuth } from "@clerk/nextjs/server";
import type { AttendeeAttributeValue } from "@prisma/client";
import { NextApiRequest, NextApiResponse } from "next";
import { v4 as uuidv4 } from "uuid";
import { getHackathon } from "..";

function c<T>(x: T): T {
console.log(x);
Expand All @@ -16,8 +17,8 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res, {attendeeAttributes: true}) as HackathonWithAttributes | null
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });

try {
let newData = req.body as { shape: Column[]; content: string[][] };
Expand All @@ -31,13 +32,7 @@ export default async function handler(

if (hackathon == null) throw new Error("no hackathon!");

const attendeeAttributes = await prisma.attendeeAttribute.findMany({
where: {
hackathon: {
slug: slug as string
}
}
});
const attendeeAttributes = hackathon.attendeeAttributes

const toCreateAttributes = newData.shape
.map((column, i) => ({
Expand Down
5 changes: 3 additions & 2 deletions pages/api/organizer/hackathons/[slug]/data/schema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
import { getHackathon } from "..";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });
let attributes: {
[key: string]: { name?: string; type?: string; options?: string[] };
} = {};
Expand Down
5 changes: 3 additions & 2 deletions pages/api/organizer/hackathons/[slug]/features/leads.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
import { getHackathon } from "..";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });
try {
switch (req.method) {
case "GET":
Expand Down
5 changes: 3 additions & 2 deletions pages/api/organizer/hackathons/[slug]/features/sponsors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
import { getHackathon } from "..";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });
const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });
try {
switch (req.method) {
case "GET":
Expand Down
30 changes: 26 additions & 4 deletions pages/api/organizer/hackathons/[slug]/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import prisma from "@/lib/prisma";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
import { HackathonPolicy } from "@/lib/permissions";

export async function getHackathon(req: NextApiRequest, res: NextApiResponse, include?: any) {
const { userId } = getAuth(req);
if (!userId) return null;
const { slug } = req.query;
const hackathon = await prisma.hackathon.delete({
where: {
slug: slug as string
},
include: {
attendees: true
}
});
if (
hackathon == null ||
userId == null ||
!new HackathonPolicy(hackathon).canOrganizerAccess({ id: userId })
) {
return null;
}
return hackathon;
}

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { userId } = getAuth(req);
if (!userId) return res.status(401).json({ error: "Unauthorized" });

const hackathon = await getHackathon(req, res)
if (!hackathon) return res.status(401).json({ error: "Unauthorized" });
switch (req.method) {
case "DELETE": {
const { slug } = req.query;
const hackathon = await prisma.hackathon.delete({
await prisma.hackathon.delete({
where: {
slug: slug as string
}
Expand Down
2 changes: 0 additions & 2 deletions pages/api/organizer/hackathons/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export default async function handler(
}
});

console.log({ hackathon });

res.redirect(`/${hackathon.slug}`);
} catch (error) {
console.error(error);
Expand Down

0 comments on commit f503e61

Please sign in to comment.