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.

Verification for email changes in partner profiles

Kiran Kecb4b10887ad
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/page-client.tsx
index 1e1f3095e3..8c80a4651f 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/page-client.tsx
@@ -205,8 +205,14 @@ function ProfileForm({
const { handleKeyDown } = useEnterSubmit();
const { executeAsync } = useAction(updatePartnerProfileAction, {
- onSuccess: async () => {
- toast.success("Profile updated successfully.");
+ onSuccess: async ({ data }) => {
+ if (data?.needsEmailVerification) {
+ toast.success(
+ "Please check your email to verify your new email address.",
+ );
+ } else {
+ toast.success("Your profile has been updated.");
+ }
},
onError({ error }) {
setError("root.serverError", {
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 10820532f0..5585475c73 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -1,22 +1,12 @@
import { DubApiError } from "@/lib/api/errors";
-import { hashToken, withSession } from "@/lib/auth";
+import { withSession } from "@/lib/auth";
+import { confirmEmailChange } from "@/lib/auth/confirm-email-change";
import { storage } from "@/lib/storage";
-import { ratelimit, redis } from "@/lib/upstash";
import { uploadedImageSchema } from "@/lib/zod/schemas/misc";
-import { sendEmail } from "@dub/email";
import { unsubscribe } from "@dub/email/resend/unsubscribe";
-import ConfirmEmailChange from "@dub/email/templates/confirm-email-change";
import { prisma } from "@dub/prisma";
-import {
- APP_DOMAIN,
- APP_HOSTNAMES,
- PARTNERS_DOMAIN,
- R2_URL,
- nanoid,
- trim,
-} from "@dub/utils";
+import { R2_URL, nanoid, trim } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
-import { randomBytes } from "crypto";
import { NextResponse } from "next/server";
import { z } from "zod";
@@ -113,56 +103,11 @@ export const PATCH = withSession(async ({ req, session }) => {
});
}
- const { success } = await ratelimit(10, "1 d").limit(
- `email-change-request:${session.user.id}`,
- );
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message:
- "You've requested too many email change requests. Please try again later.",
- });
- }
-
- const token = randomBytes(32).toString("hex");
- const expiresIn = 15 * 60 * 1000;
-
- await prisma.verificationToken.create({
- data: {
- identifier: session.user.id,
- token: await hashToken(token, { secret: true }),
- expires: new Date(Date.now() + expiresIn),
- },
+ await confirmEmailChange({
+ email: session.user.email,
+ newEmail: email,
+ identifier: session.user.id,
});
-
- await redis.set(
- `email-change-request:user:${session.user.id}`,
- {
- email: session.user.email,
- newEmail: email,
- },
- {
- px: expiresIn,
- },
- );
-
- const hostName = req.headers.get("host") || "";
- const confirmUrl = APP_HOSTNAMES.has(hostName)
- ? `${APP_DOMAIN}/auth/confirm-email-change/${token}`
- : `${PARTNERS_DOMAIN}/auth/confirm-email-change/${token}`;
-
- waitUntil(
- sendEmail({
- subject: "Confirm your email address change",
- email,
- react: ConfirmEmailChange({
- email: session.user.email,
- newEmail: email,
- confirmUrl,
- }),
- }),
- );
}
const response = await prisma.user.update({
diff --git a/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx b/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
index 8066545170..bed8eb3727 100644
--- a/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
@@ -6,7 +6,7 @@ import { subscribe } from "@dub/email/resend/subscribe";
import { unsubscribe } from "@dub/email/resend/unsubscribe";
import EmailUpdated from "@dub/email/templates/email-updated";
import { prisma } from "@dub/prisma";
-import { VerificationToken } from "@dub/prisma/client";
+import { User, VerificationToken } from "@dub/prisma/client";
import { InputPassword, LoadingSpinner } from "@dub/ui";
import { waitUntil } from "@vercel/functions";
import { redirect } from "next/navigation";
@@ -80,11 +80,12 @@ const VerifyEmailChange = async ({
redirect(`/login?next=/auth/confirm-email-change/${token}`);
}
- const currentUserId = session.user.id;
+ const { id: currentUserId, defaultPartnerId } = session.user;
const data = await redis.get<{
email: string;
newEmail: string;
+ isPartnerProfile?: boolean;
}>(`email-change-request:user:${currentUserId}`);
if (!data) {
@@ -97,23 +98,50 @@ const VerifyEmailChange = async ({
);
}
- const user = await prisma.user.update({
- where: {
- id: currentUserId,
- },
- data: {
- email: data.newEmail,
- },
- select: {
- subscribed: true,
- },
- });
+ let user: Pick<User, "subscribed"> | null = null;
+
+ // Update the partner profile email
+ if (data.isPartnerProfile) {
+ if (!defaultPartnerId) {
+ return (
+ <EmptyState
+ icon={InputPassword}
+ title="No Partner Profile Found"
+ description="We couldn’t find a partner profile for your account. Please make sure you’re logged in with the correct account at https://partners.dub.co"
+ />
+ );
+ }
+
+ await prisma.partner.update({
+ where: {
+ id: defaultPartnerId,
+ },
+ data: {
+ email: data.newEmail,
+ },
+ });
+ }
+
+ // Update the user email
+ else {
+ user = await prisma.user.update({
+ where: {
+ id: currentUserId,
+ },
+ data: {
+ email: data.newEmail,
+ },
+ select: {
+ subscribed: true,
+ },
+ });
+ }
waitUntil(
- Promise.all([
+ Promise.allSettled([
deleteRequest(tokenFound),
- ...(user.subscribed
+ ...(user?.subscribed
? [
unsubscribe({ email: data.email }),
subscribe({ email: data.newEmail }),
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index 3b7ae73a1b..3eb839034b 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -1,5 +1,6 @@
"use server";
+import { confirmEmailChange } from "@/lib/auth/confirm-email-change";
import { qstash } from "@/lib/cron";
import { storage } from "@/lib/storage";
import { prisma } from "@dub/prisma";
@@ -9,7 +10,7 @@ import {
deepEqual,
nanoid,
} from "@dub/utils";
-import { PartnerProfileType, Prisma } from "@prisma/client";
+import { Partner, PartnerProfileType } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { stripe } from "../../stripe";
import z from "../../zod";
@@ -51,7 +52,7 @@ export const updatePartnerProfileAction = authPartnerActionClient
const { partner } = ctx;
const {
name,
- email,
+ email: newEmail,
image,
description,
country,
@@ -59,65 +60,23 @@ export const updatePartnerProfileAction = authPartnerActionClient
companyName,
} = parsedInput;
- const emailChanged = partner.email !== email;
-
- const countryChanged =
- partner.country?.toLowerCase() !== country?.toLowerCase();
-
- const profileTypeChanged =
- partner.profileType.toLowerCase() !== profileType.toLowerCase();
-
- const companyNameChanged =
- partner.companyName?.toLowerCase() !== companyName?.toLowerCase();
-
- if (
- (emailChanged ||
- countryChanged ||
- profileTypeChanged ||
- companyNameChanged) &&
- partner.stripeConnectId
- ) {
- // Partner is not able to update their country, profile type, or company name
- // if they have already have a Stripe Express account + any sent / completed payouts
- const completedPayoutsCount = await prisma.payout.count({
- where: {
- partnerId: partner.id,
- status: {
- in: ["sent", "completed"],
- },
- },
- });
-
- if (completedPayoutsCount > 0) {
- throw new Error(
- "Since you've already received payouts on Dub, you cannot change your email, country or profile type. Please contact support to update those fields.",
- );
- }
-
- const response = await stripe.accounts.del(partner.stripeConnectId);
-
- if (response.deleted) {
- await prisma.partner.update({
- where: {
- id: partner.id,
- },
- data: {
- stripeConnectId: null,
- payoutsEnabledAt: null,
- },
- });
- }
+ // Delete the Stripe Express account if needed
+ await deleteStripeAccountIfRequired({
+ partner,
+ input: parsedInput,
+ });
+
+ let imageUrl: string | null = null;
+ let needsEmailVerification = false;
+ const emailChanged = partner.email !== newEmail;
+
+ // Upload the new image
+ if (image) {
+ const path = `partners/${partner.id}/image_${nanoid(7)}`;
+ const uploaded = await storage.upload(path, image);
+ imageUrl = uploaded.url;
}
- const imageUrl = image
- ? (
- await storage.upload(
- `partners/${partner.id}/image_${nanoid(7)}`,
- image,
- )
- ).url
- : null;
-
try {
const updatedPartner = await prisma.partner.update({
where: {
@@ -125,7 +84,6 @@ export const updatePartnerProfileAction = authPartnerActionClient
},
data: {
name,
- email,
description,
...(imageUrl && { image: imageUrl }),
country,
@@ -134,6 +92,30 @@ export const updatePartnerProfileAction = authPartnerActionClient
},
});
+ // If the email is being changed, we need to verify the new email address
+ if (emailChanged) {
+ const partnerWithEmail = await prisma.partner.findUnique({
+ where: {
+ email: newEmail,
+ },
+ });
+
+ if (partnerWithEmail) {
+ throw new Error(
+ `Email ${newEmail} is already in use. Do you want to merge your partner accounts instead? (https://d.to/merge-partners)`,
+ );
+ }
+
+ await confirmEmailChange({
+ email: partner.email!,
+ newEmail,
+ identifier: partner.id,
+ isPartnerProfile: true,
+ });
+
+ needsEmailVerification = true;
+ }
+
waitUntil(
(async () => {
const shouldExpireCache = !deepEqual(
@@ -159,17 +141,76 @@ export const updatePartnerProfileAction = authPartnerActionClient
});
})(),
);
+
+ return {
+ needsEmailVerification,
+ };
} catch (error) {
console.error(error);
- if (
- error instanceof Prisma.PrismaClientKnownRequestError &&
- error.code === "P2002"
- ) {
- throw new Error(
- "Email already in use. Do you want to merge your partner accounts instead? (https://d.to/merge-partners)",
- );
- }
throw new Error(error.message);
}
});
+
+const deleteStripeAccountIfRequired = async ({
+ partner,
+ input,
+}: {
+ partner: Partner;
+ input: z.infer<typeof updatePartnerProfileSchema>;
+}) => {
+ const emailChanged = partner.email !== input.email;
+
+ const countryChanged =
+ partner.country?.toLowerCase() !== input.country?.toLowerCase();
+
+ const profileTypeChanged =
+ partner.profileType.toLowerCase() !== input.profileType.toLowerCase();
+
+ const companyNameChanged =
+ partner.companyName?.toLowerCase() !== input.companyName?.toLowerCase();
+
+ const deleteExpressAccount =
+ (emailChanged ||
+ countryChanged ||
+ profileTypeChanged ||
+ companyNameChanged) &&
+ partner.stripeConnectId;
+
+ if (!deleteExpressAccount) {
+ return;
+ }
+
+ // Partner is not able to update their country, profile type, or company name
+ // if they have already have a Stripe Express account + any sent / completed payouts
+ const completedPayoutsCount = await prisma.payout.count({
+ where: {
+ partnerId: partner.id,
+ status: {
+ in: ["sent", "completed"],
+ },
+ },
+ });
+
+ if (completedPayoutsCount > 0) {
+ throw new Error(
+ "Since you've already received payouts on Dub, you cannot change your email, country or profile type. Please contact support to update those fields.",
+ );
+ }
+
+ if (partner.stripeConnectId) {
+ const response = await stripe.accounts.del(partner.stripeConnectId);
+
+ if (response.deleted) {
+ await prisma.partner.update({
+ where: {
+ id: partner.id,
+ },
+ data: {
+ stripeConnectId: null,
+ payoutsEnabledAt: null,
+ },
+ });
+ }
+ }
+};
diff --git a/apps/web/lib/auth/confirm-email-change.ts b/apps/web/lib/auth/confirm-email-change.ts
new file mode 100644
index 0000000000..9fb33a626c
--- /dev/null
+++ b/apps/web/lib/auth/confirm-email-change.ts
@@ -0,0 +1,80 @@
+import { sendEmail } from "@dub/email";
+import ConfirmEmailChange from "@dub/email/templates/confirm-email-change";
+import { prisma } from "@dub/prisma";
+import { APP_DOMAIN, PARTNERS_DOMAIN } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
+import { randomBytes } from "crypto";
+import { hashToken } from ".";
+import { DubApiError } from "../api/errors";
+import { ratelimit, redis } from "../upstash";
+
+// Send the OTP to confirm the email address change for existing users/partners
+export const confirmEmailChange = async ({
+ email,
+ newEmail,
+ identifier,
+ isPartnerProfile = false,
+}: {
+ email: string;
+ newEmail: string;
+ identifier: string;
+ isPartnerProfile?: boolean; // If true, the email is being changed for a partner profile
+}) => {
+ const { success } = await ratelimit(3, "1 d").limit(
+ `email-change-request:${identifier}`,
+ );
+
+ if (!success) {
+ throw new DubApiError({
+ code: "rate_limit_exceeded",
+ message:
+ "You've requested too many email change requests. Please try again later.",
+ });
+ }
+
+ // Remove existing verification tokens
+ await prisma.verificationToken.deleteMany({
+ where: {
+ identifier,
+ },
+ });
+
+ const token = randomBytes(32).toString("hex");
+ const expiresIn = 15 * 60 * 1000;
+
+ // Create a new verification token
+ await prisma.verificationToken.create({
+ data: {
+ identifier,
+ token: await hashToken(token, { secret: true }),
+ expires: new Date(Date.now() + expiresIn),
+ },
+ });
+
+ // Set the email change request in Redis, we'll use this to verify the email change in /auth/confirm-email-change/[token]
+ await redis.set(
+ `email-change-request:user:${identifier}`,
+ {
+ email,
+ newEmail,
+ ...(isPartnerProfile && { isPartnerProfile }),
+ },
+ {
+ px: expiresIn,
+ },
+ );
+
+ const confirmUrl = `${!isPartnerProfile ? APP_DOMAIN : PARTNERS_DOMAIN}/auth/confirm-email-change/${token}`;
+
+ waitUntil(
+ sendEmail({
+ subject: "Confirm your email address change",
+ email: newEmail,
+ react: ConfirmEmailChange({
+ email,
+ newEmail,
+ confirmUrl,
+ }),
+ }),
+ );
+};
diff --git a/packages/email/src/templates/confirm-email-change.tsx b/packages/email/src/templates/confirm-email-change.tsx
index 8fbd1b70ed..a39f998929 100644
--- a/packages/email/src/templates/confirm-email-change.tsx
+++ b/packages/email/src/templates/confirm-email-change.tsx
@@ -17,7 +17,7 @@ import { Footer } from "../components/footer";
export default function ConfirmEmailChange({
email = "[email protected]",
newEmail = "[email protected]",
- confirmUrl = "https://dub.co/auth/confirm-email-change",
+ confirmUrl = "https://dub.co/auth/confirm-email-change/d03324452e1ac9352954315f3ffc",
}: {
email: string;
newEmail: string;