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.

Use typed EmailChangeAuthError for confirm-email-change error handling

Kiran Ka874d55ecb45
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 7c1f729cb3..0ec569e1f7 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
@@ -2,6 +2,7 @@ import { getSession, hashToken } from "@/lib/auth";
import {
assertCanConfirmEmailChange,
deleteEmailChangeRequest,
+ EmailChangeAuthError,
EmailChangeRequestData,
} from "@/lib/auth/confirm-email-change";
import { prisma } from "@/lib/prisma";
@@ -100,16 +101,23 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
data,
});
} catch (error) {
- const message =
- error instanceof Error
- ? error.message
- : "This token is invalid. Please request a new one.";
+ if (error instanceof EmailChangeAuthError) {
+ return (
+ <EmptyState
+ icon={InputPassword}
+ title={
+ error.reason === "unauthorized" ? "Unauthorized" : "Invalid Token"
+ }
+ description={error.message}
+ />
+ );
+ }
return (
<EmptyState
icon={InputPassword}
- title={message.includes("access") ? "Unauthorized" : "Invalid Token"}
- description={message}
+ title="Something Went Wrong"
+ description="We couldn't verify your email change request. Please try again later."
/>
);
}
diff --git a/apps/web/lib/auth/confirm-email-change.ts b/apps/web/lib/auth/confirm-email-change.ts
index 1488eefcce..68e766ca73 100644
--- a/apps/web/lib/auth/confirm-email-change.ts
+++ b/apps/web/lib/auth/confirm-email-change.ts
@@ -4,6 +4,27 @@ import { VerificationToken } from "@prisma/client";
import { hashToken } from "./hash-token";
import { hasPermission } from "./partner-users/partner-user-permissions";
+export type EmailChangeAuthErrorReason = "invalid_token" | "unauthorized";
+
+const EMAIL_CHANGE_AUTH_ERROR_MESSAGES: Record<
+ EmailChangeAuthErrorReason,
+ string
+> = {
+ invalid_token: "This token is invalid. Please request a new one.",
+ unauthorized:
+ "You don't have access to update the partner profile associated with this email change request.",
+};
+
+export class EmailChangeAuthError extends Error {
+ readonly reason: EmailChangeAuthErrorReason;
+
+ constructor(reason: EmailChangeAuthErrorReason) {
+ super(EMAIL_CHANGE_AUTH_ERROR_MESSAGES[reason]);
+ this.name = "EmailChangeAuthError";
+ this.reason = reason;
+ }
+}
+
export type EmailChangeRequestData = {
email: string;
newEmail: string;
@@ -55,15 +76,15 @@ export async function assertCanConfirmEmailChange({
!partnerUser ||
!hasPermission(partnerUser.role, "partner_profile.update")
) {
- throw new Error("This token is invalid. Please request a new one.");
+ throw new EmailChangeAuthError("invalid_token");
}
} else if (tokenFound.identifier !== userId) {
- throw new Error("This token is invalid. Please request a new one.");
+ throw new EmailChangeAuthError("invalid_token");
}
if (data.syncIdentity) {
if (!data.partnerId) {
- throw new Error("This token is invalid. Please request a new one.");
+ throw new EmailChangeAuthError("invalid_token");
}
const partnerUser = await prisma.partnerUser.findUnique({
@@ -82,9 +103,7 @@ export async function assertCanConfirmEmailChange({
!partnerUser ||
!hasPermission(partnerUser.role, "partner_profile.update")
) {
- throw new Error(
- "You don't have access to update the partner profile associated with this email change request.",
- );
+ throw new EmailChangeAuthError("unauthorized");
}
}
}