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.

prevent viewing other link stats inside app

Steven Teyd794aac9e6bf
diff --git a/lib/planetscale.ts b/lib/planetscale.ts
index 16d64d30ef..b325d7cc39 100644
--- a/lib/planetscale.ts
+++ b/lib/planetscale.ts
@@ -5,3 +5,21 @@ export const pscale_config = {
};
export const conn = connect(pscale_config);
+
+export async function getLinkViaEdge(domain: string, key: string) {
+ const { rows } =
+ (await conn.execute(
+ "SELECT `key`, url, clicks, userId, publicStats FROM Link WHERE domain = ? AND `key` = ?",
+ [domain, key],
+ )) || {};
+
+ return rows && Array.isArray(rows) && rows.length > 0
+ ? (rows[0] as {
+ key: string;
+ url: string;
+ clicks: number;
+ userId: number;
+ publicStats: boolean;
+ })
+ : null;
+}
diff --git a/lib/swr/use-link.ts b/lib/swr/use-link.ts
new file mode 100644
index 0000000000..9603cb4fbc
--- /dev/null
+++ b/lib/swr/use-link.ts
@@ -0,0 +1,35 @@
+import { useRouter } from "next/router";
+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();
+
+ const { key } = router.query as {
+ key: string;
+ };
+
+ const { data: session } = useSession();
+
+ const { data: link } = useSWR<LinkProps>(
+ key && `/api/edge/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,
+ };
+}
diff --git a/pages/api/edge/links/[key]/index.ts b/pages/api/edge/links/[key]/index.ts
new file mode 100644
index 0000000000..fefdf49963
--- /dev/null
+++ b/pages/api/edge/links/[key]/index.ts
@@ -0,0 +1,19 @@
+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/app/links/[key].tsx b/pages/app/links/[key].tsx
index b3924cedd6..24da0e75cc 100644
--- a/pages/app/links/[key].tsx
+++ b/pages/app/links/[key].tsx
@@ -1,7 +1,16 @@
+import ErrorPage from "next/error";
import AppLayout from "@/components/layout/app";
import Stats from "@/components/stats";
+import useLink from "@/lib/swr/use-link";
export default function StatsPage() {
+ const { isOwner } = useLink();
+
+ // if not owner, show 404 page
+ if (!isOwner) {
+ return <ErrorPage statusCode={404} />;
+ }
+
return (
<AppLayout>
<Stats />