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.

create public token

Kiran K6596e3945d39
diff --git a/apps/web/app/api/referrals/tokens/route.ts b/apps/web/app/api/referrals/tokens/route.ts
new file mode 100644
index 0000000000..8643ad9046
--- /dev/null
+++ b/apps/web/app/api/referrals/tokens/route.ts
@@ -0,0 +1,47 @@
+import { DubApiError } from "@/lib/api/errors";
+import { parseRequestBody } from "@/lib/api/utils";
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import {
+ REFERRAL_PUBLIC_TOKEN_EXPIRY,
+ REFERRAL_PUBLIC_TOKEN_LENGTH,
+} from "@/lib/referrals/constants";
+import {
+ createReferralTokenSchema,
+ referralTokenSchema,
+} from "@/lib/zod/schemas/referrals";
+import { nanoid } from "@dub/utils";
+import { NextResponse } from "next/server";
+
+// GET /api/referrals/tokens - create a new referral token for the given link
+export const POST = withWorkspace(async ({ workspace, req }) => {
+ const { linkId } = createReferralTokenSchema.parse(
+ await parseRequestBody(req),
+ );
+
+ const link = await prisma.link.findUniqueOrThrow({
+ where: {
+ id: linkId,
+ projectId: workspace.id,
+ },
+ });
+
+ if (!link.trackConversion) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Conversion tracking is not enabled for this link.",
+ });
+ }
+
+ const referralToken = await prisma.referralPublicToken.create({
+ data: {
+ linkId,
+ expires: new Date(Date.now() + REFERRAL_PUBLIC_TOKEN_EXPIRY),
+ publicToken: nanoid(REFERRAL_PUBLIC_TOKEN_LENGTH),
+ },
+ });
+
+ return NextResponse.json(referralTokenSchema.parse(referralToken), {
+ status: 201,
+ });
+});
diff --git a/apps/web/lib/referrals/constants.ts b/apps/web/lib/referrals/constants.ts
index c5793bb0b2..7bda65072e 100644
--- a/apps/web/lib/referrals/constants.ts
+++ b/apps/web/lib/referrals/constants.ts
@@ -2,3 +2,7 @@ export const REFERRAL_SIGNUPS_MAX = 32;
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
diff --git a/apps/web/lib/zod/schemas/referrals.ts b/apps/web/lib/zod/schemas/referrals.ts
new file mode 100644
index 0000000000..27bc116e1a
--- /dev/null
+++ b/apps/web/lib/zod/schemas/referrals.ts
@@ -0,0 +1,10 @@
+import z from "@/lib/zod";
+
+export const createReferralTokenSchema = z.object({
+ linkId: z.string().min(1),
+});
+
+export const referralTokenSchema = z.object({
+ publicToken: z.string(),
+ expires: z.date(),
+});
diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prisma
index fd921135f6..ce45a28bed 100644
--- a/apps/web/prisma/schema/link.prisma
+++ b/apps/web/prisma/schema/link.prisma
@@ -62,6 +62,8 @@ model Link {
// Comments on the particular shortlink
comments String? @db.LongText
+ referralPublicTokens ReferralPublicToken[]
+
@@unique([domain, key])
@@unique([projectId, externalId])
@@index(projectId)
diff --git a/apps/web/prisma/schema/referral.prisma b/apps/web/prisma/schema/referral.prisma
new file mode 100644
index 0000000000..93aef82b67
--- /dev/null
+++ b/apps/web/prisma/schema/referral.prisma
@@ -0,0 +1,12 @@
+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])
+}