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.

display error message

Kiran Kc06adccd9aef
diff --git a/apps/web/app/(ee)/api/paypal/callback/route.ts b/apps/web/app/(ee)/api/paypal/callback/route.ts
index 6efbba4f6e..dd95248543 100644
--- a/apps/web/app/(ee)/api/paypal/callback/route.ts
+++ b/apps/web/app/(ee)/api/paypal/callback/route.ts
@@ -1,4 +1,3 @@
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { getSession } from "@/lib/auth";
import { paypalOAuth } from "@/lib/paypal/oauth";
import { prisma } from "@dub/prisma";
@@ -26,22 +25,17 @@ export const GET = async (req: Request) => {
);
}
- try {
- if (!session?.user.id) {
- throw new DubApiError({
- code: "unauthorized",
- message:
- "Unauthorized. You must be logged in https://partners.dub.co to continue.",
- });
- }
+ if (!session?.user.id) {
+ redirect(`${PARTNERS_DOMAIN}/login`);
+ }
+
+ let error: string | null = null;
+ try {
const { defaultPartnerId } = session.user;
if (!defaultPartnerId) {
- throw new DubApiError({
- code: "not_found",
- message: "Partner profile not found.",
- });
+ throw new Error("partner_not_found");
}
const { code, state } = oAuthCallbackSchema.parse(getSearchParams(req.url));
@@ -52,10 +46,7 @@ export const GET = async (req: Request) => {
});
if (!isStateValid) {
- throw new DubApiError({
- code: "bad_request",
- message: "Invalid state",
- });
+ throw new Error("invalid_state");
}
const accessToken = await paypalOAuth.exchangeCodeForToken({
@@ -67,11 +58,7 @@ export const GET = async (req: Request) => {
});
if (!paypalUser.email_verified) {
- throw new DubApiError({
- code: "bad_request",
- message:
- "PayPal email address is not verified. Please verify your email address in PayPal and try again https://partners.dub.co",
- });
+ throw new Error("paypal_email_not_verified");
}
const { partner } = await prisma.partnerUser.findUniqueOrThrow({
@@ -101,8 +88,14 @@ export const GET = async (req: Request) => {
// TODO:
// Send an email to the partner to inform them that their PayPal account has been connected
} catch (e) {
- return handleAndReturnErrorResponse(e);
+ console.error(e);
+
+ if (e instanceof Error) {
+ error = e.message;
+ }
}
- redirect("/settings/payouts");
+ redirect(
+ `/settings/payouts${error ? `?error=${encodeURIComponent(error)}` : ""}`,
+ );
};
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/auth.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/auth.tsx
index 1b592ddbd1..1de2a4ce38 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/auth.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/auth.tsx
@@ -3,14 +3,33 @@
import usePartnerProfile from "@/lib/swr/use-partner-profile";
import useRefreshSession from "@/lib/swr/use-refresh-session";
import LayoutLoader from "@/ui/layout/layout-loader";
-import { redirect } from "next/navigation";
-import { ReactNode } from "react";
+import { redirect, useSearchParams } from "next/navigation";
+import { ReactNode, useEffect } from "react";
+import { toast } from "sonner";
+
+const ERROR_CODES = {
+ unauthorized:
+ "Unauthorized. You must be logged in https://partners.dub.co to continue.",
+ partner_not_found: "Partner profile not found.",
+ invalid_state:
+ "Invalid or expired state. Please try again from the beginning.",
+ paypal_email_not_verified:
+ "PayPal email address is not verified. Please verify your email address in PayPal and try again.",
+} as const;
export function PartnerProfileAuth({ children }: { children: ReactNode }) {
+ const searchParams = useSearchParams();
const { loading: sessionLoading } = useRefreshSession("defaultPartnerId");
-
const { loading: partnerLoading, error } = usePartnerProfile();
+ useEffect(() => {
+ const error = searchParams?.get("error");
+
+ if (error) {
+ toast.error(ERROR_CODES[error] || "An unexpected error occurred.");
+ }
+ }, [searchParams]);
+
const loading = sessionLoading || partnerLoading;
if (loading) {