Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add dub integration

Marc Seitze1f6c273eed9
diff --git a/lib/dub.ts b/lib/dub.ts
new file mode 100644
index 00000000..59acf19b
--- /dev/null
+++ b/lib/dub.ts
@@ -0,0 +1,5 @@
+import { Dub } from "dub";
+
+export const dub = new Dub({
+ token: process.env.DUB_API_KEY,
+});
diff --git a/package-lock.json b/package-lock.json
index d057b615..2d9103e0 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -74,6 +74,7 @@
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"date-fns": "^3.6.0",
+ "dub": "^0.66.1",
"eslint": "8.57.0",
"eslint-config-next": "^14.2.31",
"exceljs": "^4.4.0",
@@ -8811,6 +8812,14 @@
"url": "https://github.com/fb55/domutils?sponsor=1"
}
},
+ "node_modules/dub": {
+ "version": "0.66.1",
+ "resolved": "https://registry.npmjs.org/dub/-/dub-0.66.1.tgz",
+ "integrity": "sha512-a3XlK1R7Pnfj33sB7Hpcl0lidrX3Rq46ZTQN48eCoMj+2QIumWoOZsEnAIP6qfEtvGuNCaDfCzfMh0Im2zyo5g==",
+ "dependencies": {
+ "zod": "^3.20.0"
+ }
+ },
"node_modules/dunder-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
@@ -17526,6 +17535,21 @@
"type": "github",
"url": "https://github.com/sponsors/wooorm"
}
+ },
+ "node_modules/react-email/node_modules/@next/swc-win32-ia32-msvc": {
+ "version": "14.2.31",
+ "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.31.tgz",
+ "integrity": "sha512-pk9Bu4K0015anTS1OS9d/SpS0UtRObC+xe93fwnm7Gvqbv/W1ZbzhK4nvc96RURIQOux3P/bBH316xz8wjGSsA==",
+ "cpu": [
+ "ia32"
+ ],
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">= 10"
+ }
}
}
}
diff --git a/package.json b/package.json
index 16a593e7..db820168 100644
--- a/package.json
+++ b/package.json
@@ -85,6 +85,7 @@
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"date-fns": "^3.6.0",
+ "dub": "^0.66.1",
"eslint": "8.57.0",
"eslint-config-next": "^14.2.31",
"exceljs": "^4.4.0",
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts
index 4b4639f1..65e3acc0 100644
--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -1,3 +1,5 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import PasskeyProvider from "@teamhanko/passkeys-next-auth-provider";
import NextAuth, { type NextAuthOptions } from "next-auth";
@@ -6,6 +8,7 @@ import GoogleProvider from "next-auth/providers/google";
import LinkedInProvider from "next-auth/providers/linkedin";
import { identifyUser, trackAnalytics } from "@/lib/analytics";
+import { dub } from "@/lib/dub";
import { isBlacklistedEmail } from "@/lib/edge-config/blacklist";
import { sendVerificationRequestEmail } from "@/lib/emails/send-verification-request";
import { sendWelcomeEmail } from "@/lib/emails/send-welcome";
@@ -197,6 +200,7 @@ export const authOptions: NextAuthOptions = {
await subscribe(message.user.email);
}
},
+ // INFO: modified below function to track leads
async signIn(message) {
if (typeof window !== "undefined") {
try {
@@ -214,4 +218,43 @@ export const authOptions: NextAuthOptions = {
},
};
-export default NextAuth(authOptions);
+const getAuthOptions = (req: NextApiRequest): NextAuthOptions => {
+ return {
+ ...authOptions,
+ events: {
+ ...authOptions.events,
+ signIn: async (message) => {
+ if (typeof window !== "undefined") {
+ try {
+ await fetch("/api/auth-plus/set-cookie");
+ } catch (error) {
+ console.error("Failed to set additional cookie", error);
+ }
+ }
+ await identifyUser(message.user.email ?? message.user.id);
+ await trackAnalytics({
+ event: "User Signed In",
+ email: message.user.email,
+ });
+
+ if (message.isNewUser) {
+ const { dub_id } = req.cookies;
+ if (dub_id) {
+ await dub.track.lead({
+ clickId: dub_id,
+ eventName: "Sign Up",
+ customerExternalId: message.user.id,
+ customerName: message.user.name,
+ customerEmail: message.user.email,
+ customerAvatar: message.user.image,
+ });
+ }
+ }
+ },
+ },
+ };
+};
+
+export default function handler(req: NextApiRequest, res: NextApiResponse) {
+ return NextAuth(req, res, getAuthOptions(req));
+}
diff --git a/pages/api/teams/[teamId]/billing/upgrade.ts b/pages/api/teams/[teamId]/billing/upgrade.ts
index d168ac57..079bbd10 100644
--- a/pages/api/teams/[teamId]/billing/upgrade.ts
+++ b/pages/api/teams/[teamId]/billing/upgrade.ts
@@ -113,6 +113,9 @@ export default async function handle(
mode: "subscription",
allow_promotion_codes: true,
client_reference_id: teamId,
+ metadata: {
+ dubCustomerId: userId,
+ },
});
}