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.

withEmbedToken

Steven Tey2a379b31bfb9
diff --git a/apps/web/app/api/analytics/client/route.ts b/apps/web/app/api/analytics/client/route.ts
index ba9983b50c..b1ec9ad0a2 100644
--- a/apps/web/app/api/analytics/client/route.ts
+++ b/apps/web/app/api/analytics/client/route.ts
@@ -1,6 +1,6 @@
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { calculateEarnings } from "@/lib/api/sales/commission";
-import { withEmbedToken } from "@/lib/referrals/auth";
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
diff --git a/apps/web/app/api/events/client/route.ts b/apps/web/app/api/events/client/route.ts
index 582e6de0ff..e447cd2b47 100644
--- a/apps/web/app/api/events/client/route.ts
+++ b/apps/web/app/api/events/client/route.ts
@@ -1,6 +1,6 @@
import { getEvents } from "@/lib/analytics/get-events";
import { calculateEarnings } from "@/lib/api/sales/commission";
-import { withEmbedToken } from "@/lib/referrals/auth";
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { eventsQuerySchema } from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
diff --git a/apps/web/app/api/referrals/invite/route.ts b/apps/web/app/api/referrals/invite/route.ts
index 01f7d24384..3539668ff0 100644
--- a/apps/web/app/api/referrals/invite/route.ts
+++ b/apps/web/app/api/referrals/invite/route.ts
@@ -1,8 +1,8 @@
import { invitePartner } from "@/lib/api/partners/invite-partner";
import { parseRequestBody } from "@/lib/api/utils";
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { requestPartnerInviteSchema } from "@/lib/dots/schemas";
import { prisma } from "@/lib/prisma";
-import { withEmbedToken } from "@/lib/referrals/auth";
import { NextResponse } from "next/server";
// GET /api/referrals/invite - check if a partner is already invited to a program
diff --git a/apps/web/app/api/referrals/link/route.ts b/apps/web/app/api/referrals/link/route.ts
index cde10416cf..2452fa5c93 100644
--- a/apps/web/app/api/referrals/link/route.ts
+++ b/apps/web/app/api/referrals/link/route.ts
@@ -1,4 +1,4 @@
-import { withEmbedToken } from "@/lib/referrals/auth";
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { NextResponse } from "next/server";
// GET /api/referrals/link - get the link for the given affiliate
diff --git a/apps/web/app/api/referrals/program/route.ts b/apps/web/app/api/referrals/program/route.ts
index 9d73d2c060..0ec7ded0b6 100644
--- a/apps/web/app/api/referrals/program/route.ts
+++ b/apps/web/app/api/referrals/program/route.ts
@@ -1,4 +1,4 @@
-import { withEmbedToken } from "@/lib/referrals/auth";
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { ProgramSchema } from "@/lib/zod/schemas/programs";
import { NextResponse } from "next/server";
diff --git a/apps/web/app/api/referrals/sales/count/route.ts b/apps/web/app/api/referrals/sales/count/route.ts
index d863ce6c45..1231b3d801 100644
--- a/apps/web/app/api/referrals/sales/count/route.ts
+++ b/apps/web/app/api/referrals/sales/count/route.ts
@@ -1,6 +1,6 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { prisma } from "@/lib/prisma";
-import { withEmbedToken } from "@/lib/referrals/auth";
import { getPartnerSalesCountQuerySchema } from "@/lib/zod/schemas/partners";
import { NextResponse } from "next/server";
diff --git a/apps/web/app/api/referrals/sales/route.ts b/apps/web/app/api/referrals/sales/route.ts
index 5737b8bc9b..a1208cf7eb 100644
--- a/apps/web/app/api/referrals/sales/route.ts
+++ b/apps/web/app/api/referrals/sales/route.ts
@@ -1,5 +1,5 @@
+import { withEmbedToken } from "@/lib/auth/embed-token";
import { prisma } from "@/lib/prisma";
-import { withEmbedToken } from "@/lib/referrals/auth";
import z from "@/lib/zod";
import { PartnerSaleResponseSchema } from "@/lib/zod/schemas/partners";
import { NextResponse } from "next/server";
diff --git a/apps/web/app/api/referrals/tokens/route.ts b/apps/web/app/api/tokens/embed/route.ts
similarity index 69%
rename from apps/web/app/api/referrals/tokens/route.ts
rename to apps/web/app/api/tokens/embed/route.ts
index ea5e77dd69..732c0b3306 100644
--- a/apps/web/app/api/referrals/tokens/route.ts
+++ b/apps/web/app/api/tokens/embed/route.ts
@@ -1,3 +1,4 @@
+import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { createEmbedToken } from "@/lib/referrals/create-embed-token";
@@ -7,13 +8,14 @@ import {
} from "@/lib/zod/schemas/referrals";
import { NextResponse } from "next/server";
-// GET /api/referrals/tokens - create a new referral token for the given link
+// POST /api/tokens/embed - create a new embed token for the given link
export const POST = withWorkspace(async ({ workspace, req }) => {
const { linkId } = createReferralTokenSchema.parse(
await parseRequestBody(req),
);
+ await getLinkOrThrow({ linkId, workspaceId: workspace.id });
- const token = await createEmbedToken({ linkId, workspaceId: workspace.id });
+ const token = await createEmbedToken(linkId);
return NextResponse.json(referralTokenSchema.parse(token), {
status: 201,
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/embed-token/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/embed-token/route.ts
index 17ccca2fad..4a0492cd5c 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/embed-token/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/embed-token/route.ts
@@ -14,7 +14,7 @@ export const POST = withWorkspace(async ({ workspace }) => {
});
}
- const token = await fetch(`${APP_DOMAIN_WITH_NGROK}/api/referrals/tokens`, {
+ const token = await fetch(`${APP_DOMAIN_WITH_NGROK}/api/tokens/embed`, {
method: "POST",
headers: {
"Content-Type": "application/json",
diff --git a/apps/web/lib/referrals/auth.ts b/apps/web/lib/auth/embed-token.ts
similarity index 97%
rename from apps/web/lib/referrals/auth.ts
rename to apps/web/lib/auth/embed-token.ts
index 0228f6b57e..4bc4c1c4ae 100644
--- a/apps/web/lib/referrals/auth.ts
+++ b/apps/web/lib/auth/embed-token.ts
@@ -5,7 +5,7 @@ import { getSearchParams } from "@dub/utils";
import { Link, Program, Project } from "@prisma/client";
import { AxiomRequest, withAxiom } from "next-axiom";
import { cookies } from "next/headers";
-import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "./constants";
+import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "../referrals/constants";
interface WithEmbedTokenHandler {
({
diff --git a/apps/web/lib/referrals/create-embed-token.ts b/apps/web/lib/referrals/create-embed-token.ts
index 16551933ca..6fe8ac3889 100644
--- a/apps/web/lib/referrals/create-embed-token.ts
+++ b/apps/web/lib/referrals/create-embed-token.ts
@@ -1,7 +1,6 @@
import { prisma } from "@/lib/prisma";
import { waitUntil } from "@vercel/functions";
import { DubApiError } from "../api/errors";
-import { getLinkOrThrow } from "../api/links/get-link-or-throw";
import { createId } from "../api/utils";
import { ratelimit } from "../upstash";
import {
@@ -9,16 +8,7 @@ import {
EMBED_PUBLIC_TOKEN_LENGTH,
} from "./constants";
-export const createEmbedToken = async ({
- linkId,
- workspaceId,
-}: {
- linkId: string;
- workspaceId: string;
-}) => {
- const link = await getLinkOrThrow({ linkId, workspaceId });
- console.log("link", link);
-
+export const createEmbedToken = async (linkId: string) => {
const { success } = await ratelimit(10, "1 m").limit(linkId);
if (!success) {