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.

update the routes

Kiran K826b3e92873a
diff --git a/apps/web/app/app.dub.co/embed/referrals.tsx b/apps/web/app/app.dub.co/embed/referrals.tsx
index 182e3df5a3..7eefb96e19 100644
--- a/apps/web/app/app.dub.co/embed/referrals.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals.tsx
@@ -19,19 +19,17 @@ export const Referrals = ({ event, page, publicToken }: ReferralsProps) => {
<h2 className="mb-2 text-2xl font-semibold text-gray-800">
Unavailable
</h2>
- <p className="text-gray-600">The referral token is not found.</p>
+ <p className="text-gray-600">The embed public token is not found.</p>
</div>
);
}
return (
<DubProvider publicToken={publicToken}>
- {/* Stats */}
<div className="mt-8">
<Stats />
</div>
- {/* Events */}
<div className="mt-12">
<div className="mb-5 flex flex-wrap items-end justify-between gap-4">
<h2 className="text-xl font-semibold text-gray-800">Activity</h2>
diff --git a/apps/web/lib/middleware/app.ts b/apps/web/lib/middleware/app.ts
index ee21346047..812b7bb94c 100644
--- a/apps/web/lib/middleware/app.ts
+++ b/apps/web/lib/middleware/app.ts
@@ -1,5 +1,6 @@
import { parse } from "@/lib/middleware/utils";
import { NextRequest, NextResponse } from "next/server";
+import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "../referrals/constants";
import NewLinkMiddleware from "./new-link";
import { getDefaultWorkspace } from "./utils/get-default-workspace";
import { getOnboardingStep } from "./utils/get-onboarding-step";
@@ -13,11 +14,11 @@ export default async function AppMiddleware(req: NextRequest) {
const isWorkspaceInvite = req.nextUrl.searchParams.get("invite");
if (path.startsWith("/embed")) {
+ const token = req.nextUrl.searchParams.get("token");
+
return NextResponse.rewrite(new URL(`/app.dub.co${fullPath}`, req.url), {
headers: {
- // TODO: Need better cookie name
- // Maybe move this to a API route level?
- "Set-Cookie": `token=${req.nextUrl.searchParams.get("token")}; HttpOnly; Path=/`,
+ "Set-Cookie": `${EMBED_PUBLIC_TOKEN_COOKIE_NAME}=${token}; HttpOnly; Path=/`,
},
});
}
diff --git a/apps/web/lib/referrals/auth.ts b/apps/web/lib/referrals/auth.ts
index b73cc9d551..cd1b89dbc4 100644
--- a/apps/web/lib/referrals/auth.ts
+++ b/apps/web/lib/referrals/auth.ts
@@ -5,6 +5,7 @@ import { getSearchParams } from "@dub/utils";
import { Link, Project } from "@prisma/client";
import { AxiomRequest, withAxiom } from "next-axiom";
import { cookies } from "next/headers";
+import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "./constants";
interface WithAuthHandler {
({
@@ -32,58 +33,23 @@ export const withAuth = (handler: WithAuthHandler) => {
try {
let link: Link | undefined = undefined;
- let tokenFromHeader: string | undefined = undefined;
const rateLimit = 60;
const searchParams = getSearchParams(req.url);
- // const authorizationHeader = req.headers.get("Authorization");
-
- // if (!authorizationHeader) {
- // throw new DubApiError({
- // code: "unauthorized",
- // message: "Missing Authorization header.",
- // });
- // }
-
- // if (!authorizationHeader.includes("Bearer ")) {
- // throw new DubApiError({
- // code: "bad_request",
- // message:
- // "Misconfigured authorization header. Did you forget to add 'Bearer '? Learn more: https://d.to/auth",
- // });
- // }
-
- // tokenFromHeader = authorizationHeader.replace("Bearer ", "");
-
- // if (!tokenFromHeader) {
- // throw new DubApiError({
- // code: "unauthorized",
- // message: "Missing Authorization header.",
- // });
- // }
-
- // Read token from query params
- // const tokenFromQuery = searchParams["publicToken"];
-
- // if (!tokenFromQuery) {
- // throw new DubApiError({
- // code: "unauthorized",
- // message: "Missing public token.",
- // });
- // }
-
const cookieStore = cookies();
- const tokenFromCookie = cookieStore.get("token")?.value;
+ const tokenFromCookie = cookieStore.get(
+ EMBED_PUBLIC_TOKEN_COOKIE_NAME,
+ )?.value;
if (!tokenFromCookie) {
throw new DubApiError({
code: "unauthorized",
- message: "Missing public token.",
+ message: "Embed public token not found in the request.",
});
}
- const publicToken = await prisma.referralPublicToken.findUnique({
+ const publicToken = await prisma.embedPublicToken.findUnique({
where: {
publicToken: tokenFromCookie,
},
diff --git a/apps/web/lib/referrals/constants.ts b/apps/web/lib/referrals/constants.ts
index 7bda65072e..0a22f39907 100644
--- a/apps/web/lib/referrals/constants.ts
+++ b/apps/web/lib/referrals/constants.ts
@@ -3,6 +3,7 @@ export const REFERRAL_CLICKS_QUOTA_BONUS = 500;
export const REFERRAL_CLICKS_QUOTA_BONUS_MAX = 16000;
export const REFERRAL_REVENUE_SHARE = 0.2;
-// Referral public token
-export const REFERRAL_PUBLIC_TOKEN_LENGTH = 36;
-export const REFERRAL_PUBLIC_TOKEN_EXPIRY = 1000 * 60 * 60 * 2; // 2 hours
+// Embed public token
+export const EMBED_PUBLIC_TOKEN_LENGTH = 36;
+export const EMBED_PUBLIC_TOKEN_EXPIRY = 1000 * 60 * 60 * 2; // 2 hours
+export const EMBED_PUBLIC_TOKEN_COOKIE_NAME = "embed_public_token";
diff --git a/apps/web/lib/referrals/token.ts b/apps/web/lib/referrals/token.ts
index 04e8638ec4..76c9e1ae6b 100644
--- a/apps/web/lib/referrals/token.ts
+++ b/apps/web/lib/referrals/token.ts
@@ -2,8 +2,8 @@ import { prisma } from "@/lib/prisma";
import { nanoid } from "@dub/utils";
import { DubApiError } from "../api/errors";
import {
- REFERRAL_PUBLIC_TOKEN_EXPIRY,
- REFERRAL_PUBLIC_TOKEN_LENGTH,
+ EMBED_PUBLIC_TOKEN_EXPIRY,
+ EMBED_PUBLIC_TOKEN_LENGTH,
} from "./constants";
export const createPublicToken = async ({
@@ -27,11 +27,11 @@ export const createPublicToken = async ({
});
}
- return await prisma.referralPublicToken.create({
+ return await prisma.embedPublicToken.create({
data: {
linkId,
- expires: new Date(Date.now() + REFERRAL_PUBLIC_TOKEN_EXPIRY),
- publicToken: nanoid(REFERRAL_PUBLIC_TOKEN_LENGTH),
+ expires: new Date(Date.now() + EMBED_PUBLIC_TOKEN_EXPIRY),
+ publicToken: nanoid(EMBED_PUBLIC_TOKEN_LENGTH),
},
});
};
diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prisma
index 769e71c694..0fc4081edf 100644
--- a/apps/web/prisma/schema/link.prisma
+++ b/apps/web/prisma/schema/link.prisma
@@ -63,7 +63,7 @@ model Link {
// Comments on the particular shortlink
comments String? @db.LongText
- referralPublicTokens ReferralPublicToken[]
+ embedPublicTokens EmbedPublicToken[]
@@unique([domain, key])
@@unique([projectId, externalId])
diff --git a/apps/web/prisma/schema/referral.prisma b/apps/web/prisma/schema/referral.prisma
deleted file mode 100644
index 93aef82b67..0000000000
--- a/apps/web/prisma/schema/referral.prisma
+++ /dev/null
@@ -1,12 +0,0 @@
-model ReferralPublicToken {
- id String @id @default(cuid())
- linkId String
- publicToken String @unique
- expires DateTime
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
-
- link Link @relation(fields: [linkId], references: [id], onDelete: Cascade)
-
- @@index([linkId])
-}
diff --git a/apps/web/prisma/schema/token.prisma b/apps/web/prisma/schema/token.prisma
index 745af51753..3f19527e5c 100644
--- a/apps/web/prisma/schema/token.prisma
+++ b/apps/web/prisma/schema/token.prisma
@@ -64,4 +64,17 @@ model PasswordResetToken {
expires DateTime
@@unique([identifier, token])
-}
\ No newline at end of file
+}
+
+model EmbedPublicToken {
+ id String @id @default(cuid())
+ linkId String
+ publicToken String @unique
+ expires DateTime
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+
+ link Link @relation(fields: [linkId], references: [id], onDelete: Cascade)
+
+ @@index([linkId])
+}