Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

added withProjectAuth to /api/projects/[slug] endpoint

Steven Teyeeed2aa892f8
diff --git a/components/layout/app/index.tsx b/components/layout/app/index.tsx
index 849e08449f..8e0beee5ec 100644
--- a/components/layout/app/index.tsx
+++ b/components/layout/app/index.tsx
@@ -7,6 +7,7 @@ import ListBox from "./list-box";
import UserDropdown from "./user-dropdown";
import { useRouter } from "next/router";
import Script from "next/script";
+import { Toaster } from "react-hot-toast";
const CRISP_SCRIPT = `window.$crisp=[];window.CRISP_WEBSITE_ID="2c09b1ee-14c2-46d1-bf72-1dbb998a19e0";(function(){d=document;s=d.createElement("script");s.src="https://client.crisp.chat/l.js";s.async=1;d.getElementsByTagName("head")[0].appendChild(s);})();`;
@@ -32,6 +33,7 @@ export default function AppLayout({ children }: { children: ReactNode }) {
}}
strategy="lazyOnload"
/>
+ <Toaster />
<div className="min-h-screen w-full bg-gray-50">
<div className="sticky top-0 left-0 right-0 border-b bg-white border-gray-200 z-20">
<div className="max-w-screen-xl mx-auto px-5 sm:px-20">
diff --git a/lib/auth.ts b/lib/auth.ts
index ad0955b430..0413b02b82 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -68,7 +68,7 @@ const withProjectAuth =
},
})) as ProjectProps;
- if (!project) return res.status(401).end("Unauthorized");
+ if (!project) return res.status(404).json({ error: "Project not found" });
if (isWriteEditLink && req.method !== "GET") {
if (!project.domainVerified) {
diff --git a/pages/api/projects/[slug]/index.ts b/pages/api/projects/[slug]/index.ts
index 9f441863a1..f03daad64c 100644
--- a/pages/api/projects/[slug]/index.ts
+++ b/pages/api/projects/[slug]/index.ts
@@ -1,65 +1,50 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { getSession } from "@/lib/auth";
+import { withProjectAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { removeDomain } from "@/lib/domains";
import { deleteProject } from "@/lib/upstash";
+import { ProjectProps } from "@/lib/types";
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse
-) {
- const session = await getSession(req, res);
- if (!session?.user.id) return res.status(401).end("Unauthorized");
-
- const { slug } = req.query;
- if (!slug || typeof slug !== "string") {
- return res
- .status(400)
- .json({ error: "Missing or misconfigured project slug" });
- }
+export default withProjectAuth(
+ async (req: NextApiRequest, res: NextApiResponse, project: ProjectProps) => {
+ const { slug } = req.query;
+ if (!slug || typeof slug !== "string") {
+ return res
+ .status(400)
+ .json({ error: "Missing or misconfigured project slug" });
+ }
- // GET /api/projects/[slug] – get a specific project with it's links
- if (req.method === "GET") {
- const project = await prisma.project.findFirst({
- where: {
- slug,
- users: {
- some: {
- userId: session.user.id,
- },
- },
- },
- });
- if (project) {
+ // GET /api/projects/[slug] – get a specific project with it's links
+ if (req.method === "GET") {
return res.status(200).json(project);
- } else {
- return res.status(404).json({ error: "Project not found" });
- }
+ // DELETE /api/projects/[slug] – edit a project
+ } else if (req.method === "DELETE") {
+ const domain = req.body;
+ if (!domain || typeof domain !== "string") {
+ return res
+ .status(400)
+ .json({ error: "Missing or misconfigured domain" });
+ }
- // DELETE /api/projects/[slug] – edit a project
- } else if (req.method === "DELETE") {
- const domain = req.body;
- if (!domain || typeof domain !== "string") {
- return res.status(400).json({ error: "Missing or misconfigured domain" });
- }
+ const [prismaResponse, domainResponse, upstashResponse] =
+ await Promise.all([
+ prisma.project.delete({
+ where: {
+ slug,
+ },
+ }),
+ removeDomain(domain),
+ deleteProject(domain),
+ ]);
- const [prismaResponse, domainResponse, upstashResponse] = await Promise.all(
- [
- prisma.project.delete({
- where: {
- slug,
- },
- }),
- removeDomain(domain),
- deleteProject(domain),
- ]
- );
- console.log(prismaResponse, domainResponse, upstashResponse);
- return res
- .status(200)
- .json({ prismaResponse, domainResponse, upstashResponse });
- } else {
- res.setHeader("Allow", ["GET", "DELETE"]);
- return res.status(405).json({ error: `Method ${req.method} Not Allowed` });
+ return res
+ .status(200)
+ .json({ prismaResponse, domainResponse, upstashResponse });
+ } else {
+ res.setHeader("Allow", ["GET", "DELETE"]);
+ return res
+ .status(405)
+ .json({ error: `Method ${req.method} Not Allowed` });
+ }
}
-}
+);