The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix the default partnerId
Kiran Kbf566dbdb138
diff --git a/apps/web/lib/auth/partner.ts b/apps/web/lib/auth/partner.tsindex c4e3fd1f17..2b85a4d8de 100644--- a/apps/web/lib/auth/partner.ts+++ b/apps/web/lib/auth/partner.ts@@ -40,7 +40,29 @@ export const withPartnerProfile = (handler: WithPartnerProfileHandler) => {}const searchParams = getSearchParams(req.url);- const { defaultPartnerId, id: userId } = session.user;+ let { defaultPartnerId, id: userId } = session.user;++ // During the partner onboarding process, the defaultPartnerId is not set in the partner object,+ if (!defaultPartnerId) {+ const partnerUser = await prisma.user.findUnique({+ where: {+ id: userId,+ },+ select: {+ defaultPartnerId: true,+ partners: {+ select: {+ partnerId: true,+ },+ take: 1,+ },+ },+ });++ defaultPartnerId =+ partnerUser?.defaultPartnerId ||+ partnerUser?.partners[0]?.partnerId;+ }if (!defaultPartnerId) {throw new DubApiError({diff --git a/apps/web/lib/middleware/partners.ts b/apps/web/lib/middleware/partners.tsindex 3627d9cbe0..4e65352956 100644--- a/apps/web/lib/middleware/partners.ts+++ b/apps/web/lib/middleware/partners.ts@@ -25,7 +25,7 @@ export default async function PartnersMiddleware(req: NextRequest) {);if (!user && isAuthenticatedPath) {- if (path.startsWith(`/programs/`)) {+ if (path.startsWith("/programs/")) {const programSlug = path.split("/")[2];return NextResponse.redirect(new URL(`/${programSlug}/login`, req.url));}diff --git a/apps/web/lib/swr/use-partner-profile.ts b/apps/web/lib/swr/use-partner-profile.tsindex c7b2eb529d..fe9698ac7c 100644--- a/apps/web/lib/swr/use-partner-profile.ts+++ b/apps/web/lib/swr/use-partner-profile.ts@@ -6,8 +6,6 @@ import { PartnerProps } from "../types";export default function usePartnerProfile() {const { data: session, status } = useSession();- const partnerId = session?.user?.["defaultPartnerId"];-const [isPartnerPage, setIsPartnerPage] = useState(false);useEffect(() => {@@ -20,7 +18,7 @@ export default function usePartnerProfile() {isLoading,mutate,} = useSWR<PartnerProps>(- isPartnerPage && partnerId && "/api/partner-profile",+ isPartnerPage && session?.user && "/api/partner-profile",fetcher,{dedupingInterval: 60000,