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.

improved rbac

Steven Tey037e89b94bf1
diff --git a/lib/swr/use-link.ts b/lib/swr/use-link.ts
index 9603cb4fbc..a0268f4744 100644
--- a/lib/swr/use-link.ts
+++ b/lib/swr/use-link.ts
@@ -3,7 +3,6 @@ import { useMemo } from "react";
import useSWR from "swr";
import { LinkProps } from "@/lib/types";
import { fetcher } from "@/lib/utils";
-import { useSession } from "next-auth/react";
export default function useLink() {
const router = useRouter();
@@ -12,24 +11,17 @@ export default function useLink() {
key: string;
};
- const { data: session } = useSession();
-
- const { data: link } = useSWR<LinkProps>(
- key && `/api/edge/links/${key}`,
+ const { data: link, error } = useSWR<LinkProps>(
+ key && `/api/links/${key}`,
fetcher,
{
dedupingInterval: 10000,
},
);
- const isOwner = useMemo(() => {
- if (!session || !link) return false;
- // @ts-ignore - TODO: fix this
- return session.user.id === link.userId;
- }, [session, link]);
-
return {
link,
- isOwner,
+ loading: !error && !link,
+ error,
};
}
diff --git a/pages/api/edge/links/[key]/index.ts b/pages/api/edge/links/[key]/index.ts
deleted file mode 100644
index fefdf49963..0000000000
--- a/pages/api/edge/links/[key]/index.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import type { NextRequest } from "next/server";
-import { getLinkViaEdge } from "@/lib/planetscale";
-
-export const config = {
- runtime: "experimental-edge",
-};
-
-export default async function handler(req: NextRequest) {
- if (req.method === "GET") {
- const key = req.nextUrl.searchParams.get("key");
- if (!key) {
- return new Response(`Missing key`, { status: 400 });
- }
- const data = await getLinkViaEdge("dub.sh", key);
- return new Response(JSON.stringify(data), { status: 200 });
- } else {
- return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
- }
-}
diff --git a/pages/api/links/[key]/index.ts b/pages/api/links/[key]/index.ts
index 445fbb9b4d..7b804dc77d 100644
--- a/pages/api/links/[key]/index.ts
+++ b/pages/api/links/[key]/index.ts
@@ -18,22 +18,24 @@ export default withUserAuth(
async (req: NextApiRequest, res: NextApiResponse, session: Session) => {
const { key: oldKey } = req.query as { key: string };
- const isOwner = await prisma.link
- .findUnique({
- where: {
- domain_key: {
- domain,
- key: oldKey,
- },
+ const link = await prisma.link.findUnique({
+ where: {
+ domain_key: {
+ domain,
+ key: oldKey,
},
- })
- .then((link) => link?.userId === session.user.id);
+ },
+ });
+
+ const isOwner = link.userId === session.user.id;
if (!isOwner) {
return res.status(403).json({ error: "Not authorized" });
}
- if (req.method === "PUT") {
+ if (req.method === "GET") {
+ return res.status(200).json(link);
+ } else if (req.method === "PUT") {
let { key, url } = req.body;
if (!key || !url) {
return res
@@ -60,7 +62,7 @@ export default withUserAuth(
const response = await deleteLink(domain, oldKey);
return res.status(200).json(response);
} else {
- res.setHeader("Allow", ["PUT", "DELETE"]);
+ res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
return res
.status(405)
.json({ error: `Method ${req.method} Not Allowed` });
diff --git a/pages/app/links/[key].tsx b/pages/app/links/[key].tsx
index 24da0e75cc..46c834f005 100644
--- a/pages/app/links/[key].tsx
+++ b/pages/app/links/[key].tsx
@@ -4,16 +4,12 @@ import Stats from "@/components/stats";
import useLink from "@/lib/swr/use-link";
export default function StatsPage() {
- const { isOwner } = useLink();
+ const { loading, error } = useLink();
// if not owner, show 404 page
- if (!isOwner) {
+ if (error) {
return <ErrorPage statusCode={404} />;
}
- return (
- <AppLayout>
- <Stats />
- </AppLayout>
- );
+ return <AppLayout>{!loading && !error && <Stats />}</AppLayout>;
}