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 nextauth.md

Kiran K2c7bfe2c3d4e
diff --git a/apps/web/integration-guides/nextauth.md b/apps/web/integration-guides/nextauth.md
index e69de29bb2..a9355fbc17 100644
--- a/apps/web/integration-guides/nextauth.md
+++ b/apps/web/integration-guides/nextauth.md
@@ -0,0 +1,47 @@
+Configure NextAuth to track lead conversion events in your app.
+
+Here's how it works in a nutshell:
+
+1. Use NextAuth's [`signIn` event](https://next-auth.js.org/configuration/events#signin) to detect when there's a new sign up.
+2. If the user is a new sign up, check if the `dub_id` cookie is present.
+3. If the `dub_id` cookie is present, send a lead event to Dub using `dub.track.lead`
+4. Delete the `dub_id` cookie.
+
+Under the hood, Dub records the user as a customer and associates them with the click event that they came from. The user's unique ID is now the source of truth for all future events – hence why we don't need the `dub_id` cookie anymore.
+
+```typescript
+// app/api/auth/[...nextauth]/options.ts
+import type { NextAuthOptions } from "next-auth";
+import { cookies } from "next/headers";
+import { dub } from "@/lib/dub";
+
+export const authOptions: NextAuthOptions = {
+ ...otherAuthOptions, // your other NextAuth options
+ events: {
+ async signIn(message) {
+ // if it's a new sign up
+ if (message.isNewUser) {
+ // check if dub_id cookie is present
+ const dub_id = cookies().get("dub_id")?.value;
+
+ if (dub_id) {
+ // send lead event to Dub
+ await dub.track.lead({
+ clickId: dub_id,
+ eventName: "Sign Up",
+ externalId: user.id,
+ customerName: user.name,
+ customerEmail: user.email,
+ customerAvatar: user.image,
+ });
+
+ // delete the dub_id cookie
+ cookies().set("dub_id", "", {
+ expires: new Date(0),
+ });
+ }
+ }
+ },
+ },
+};
+```