The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add support for `SSO_LOGIN_PROGRAMS` (#3815)
Steven Teyfb9ecad8f0cd
diff --git a/apps/web/.env.example b/apps/web/.env.exampleindex f09accde36..4de63d4ef1 100644--- a/apps/web/.env.example+++ b/apps/web/.env.example@@ -85,10 +85,6 @@ GOOGLE_CLIENT_SECRET=GITHUB_CLIENT_ID=GITHUB_CLIENT_SECRET=-# Used for Framer Login-FRAMER_CLIENT_ID=-FRAMER_CLIENT_SECRET=-# Edge Config – used for admin features like banning users, rate limiting etc.# Create one here: https://vercel.com/docs/storage/edge-config# Alternatively, if you don't need this, you can also remove `lib/edge-config.ts`diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/earnings-summary.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/earnings-summary.tsxindex 2fc0442027..6cb5507afd 100644--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/earnings-summary.tsx+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/earnings-summary.tsx@@ -1,3 +1,4 @@+import { SSO_LOGIN_PROGRAMS } from "@/lib/auth/sso-login-programs";import { Button, InfoTooltip } from "@dub/ui";import { currencyFormatter } from "@dub/utils";import { useReferralsEmbedData } from "./page-client";@@ -5,6 +6,12 @@ import { useReferralsEmbedData } from "./page-client";export function ReferralsEmbedEarningsSummary() {const { program, partner, earnings } = useReferralsEmbedData();+ // for custom SSO login programs, we just redirect to the login page+ // so they can easily login with SSO instead of creating a new account+ const isCustomSSOLoginProgram = SSO_LOGIN_PROGRAMS.some(+ ({ slug }) => slug === program.slug,+ );+return (<div className="border-border-subtle bg-bg-default flex flex-col justify-between gap-4 rounded-lg border p-4"><div className="flex items-center justify-between">@@ -13,8 +20,10 @@ export function ReferralsEmbedEarningsSummary() {<InfoTooltip content="Summary of your commission earnings from your referrals." /></div><a- href={`https://partners.dub.co/${program.slug}/register${- partner.email ? `?email=${partner.email}` : ""+ href={`https://partners.dub.co/${program.slug}/${+ isCustomSSOLoginProgram+ ? "login"+ : `register${partner.email ? `?email=${partner.email}` : ""}`}`}target="_blank">diff --git a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/cta-buttons.tsx b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/cta-buttons.tsxindex f62d5e06d1..306660eb73 100644--- a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/cta-buttons.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/cta-buttons.tsx@@ -13,7 +13,11 @@ export function CTAButtons() {return (<>- <Link href={status === "authenticated" ? "/" : `${slugPrefix}/register`}>+ <Link+ href={+ status === "authenticated" ? "/programs" : `${slugPrefix}/register`+ }+ ><Buttontype="button"text={diff --git a/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/page.tsx b/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/page.tsxindex 3102574ad7..482d47ed0f 100644--- a/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/page.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/page.tsx@@ -1,11 +1,12 @@+import { SSO_LOGIN_PROGRAMS } from "@/lib/auth/sso-login-programs";import { getProgram } from "@/lib/fetchers/get-program";import { AuthAlternativeBanner } from "@/ui/auth/auth-alternative-banner";-import { FramerButton } from "@/ui/auth/login/framer-button";import LoginForm from "@/ui/auth/login/login-form";import { AuthLayout } from "@/ui/layout/auth-layout";import { cn, constructMetadata } from "@dub/utils";import Link from "next/link";import { redirect } from "next/navigation";+import { SSOLoginButton } from "./sso-login-button";export const metadata = constructMetadata({fullTitle: "Login to partners.dub.co",@@ -16,25 +17,29 @@ export default async function LoginPage(props: {}) {const { programSlug } = await props.params;- if (programSlug === "framer") {+ const customSSOLoginProgram = programSlug+ ? SSO_LOGIN_PROGRAMS.find((program) => program.slug === programSlug)+ : undefined;++ if (customSSOLoginProgram) {return (<AuthLayout showTerms="partners"><div className="mx-auto my-10 flex w-full max-w-sm flex-col gap-8"><div className="animate-slide-up-fade relative flex w-auto flex-col items-center [--offset:10px] [animation-duration:1.3s] [animation-fill-mode:both]"><img- src="https://assets.dub.co/testimonials/companies/framer.svg"- alt="Framer Logo"+ src={customSSOLoginProgram.logo}+ alt={`${customSSOLoginProgram.name} Logo`}className="h-8"/></div><div className="animate-slide-up-fade flex flex-col items-center justify-center gap-2 [--offset:10px] [animation-delay:0.15s] [animation-duration:1.3s] [animation-fill-mode:both]"><h1 className="text-lg font-medium text-neutral-800">- Sign in to Framer Partners+ Sign in to {customSSOLoginProgram.name} Partners</h1><p className="text-center text-sm text-neutral-700">- Not a Framer Partner? + Not a {customSSOLoginProgram.name} Partner? <a- href="https://www.framer.com/creators"+ href={customSSOLoginProgram.applyUrl}target="_blank"rel="noopener noreferrer"className="font-normal underline decoration-dotted underline-offset-2 transition-colors hover:text-black"@@ -45,7 +50,10 @@ export default async function LoginPage(props: {</div><div className="animate-slide-up-fade [--offset:10px] [animation-delay:0.3s] [animation-duration:1.3s] [animation-fill-mode:both]">- <FramerButton />+ <SSOLoginButton+ name={customSSOLoginProgram.name}+ slug={customSSOLoginProgram.slug}+ /></div></div></AuthLayout>diff --git a/apps/web/ui/auth/login/framer-button.tsx b/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/sso-login-button.tsxsimilarity index 54%rename from apps/web/ui/auth/login/framer-button.tsxrename to apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/sso-login-button.tsxindex 3454ea5f2f..0c3f04603d 100644--- a/apps/web/ui/auth/login/framer-button.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/login/sso-login-button.tsx@@ -1,27 +1,31 @@"use client";-import { Button } from "@dub/ui";+import { Beehiiv, Button } from "@dub/ui";import { cn } from "@dub/utils";import { Framer } from "lucide-react";import { signIn } from "next-auth/react";import { useState } from "react";-export const FramerButton = () => {+export function SSOLoginButton({ name, slug }: { name: string; slug: string }) {const [clicked, setClicked] = useState(false);+ const Logo = {+ framer: Framer,+ beehiiv: Beehiiv,+ }[slug];return (<Button- text="Login with Framer"+ text={`Login with ${name}`}variant="secondary"onClick={() => {setClicked(true);- signIn("framer", {- callbackUrl: "/programs/framer",+ signIn(slug, {+ callbackUrl: `/programs/${slug}`,});}}- icon={<Framer className="size-4 fill-white text-white" />}+ {...(Logo && { icon: <Logo className="size-4 fill-white text-white" /> })}className={cn(!clicked && "bg-blue-600 text-white hover:bg-blue-700")}loading={clicked}/>);-};+}diff --git a/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/register/page.tsx b/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/register/page.tsxindex a28569f68f..ea90fc3e6d 100644--- a/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/register/page.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(auth-login-register)/(generic)/register/page.tsx@@ -14,10 +14,6 @@ export default async function RegisterPage(props: {}) {const { programSlug } = await props.params;- if (programSlug === "framer") {- redirect("/framer/login");- }-const program = programSlug? (await getProgram({ slug: programSlug })) ?? undefined: undefined;diff --git a/apps/web/lib/auth/constants.ts b/apps/web/lib/auth/constants.tsindex bdc55a0e6c..874daddd1b 100644--- a/apps/web/lib/auth/constants.ts+++ b/apps/web/lib/auth/constants.ts@@ -3,8 +3,3 @@ export const PASSWORD_RESET_TOKEN_EXPIRY = 1 * 60 * 60; // 1 hourexport const MAX_LOGIN_ATTEMPTS = 10;export const EMAIL_OTP_EXPIRY_IN = 5 * 60; // 5 minutes--export const FRAMER_API_HOST =- process.env.NODE_ENV === "production"- ? "https://api.framer.com"- : "https://api.development.framer.com";diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.tsindex 9f1729205f..fdd714063f 100644--- a/apps/web/lib/auth/options.ts+++ b/apps/web/lib/auth/options.ts@@ -11,7 +11,7 @@ import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";import { PrismaAdapter } from "@next-auth/prisma-adapter";import { waitUntil } from "@vercel/functions";import { User, type NextAuthOptions } from "next-auth";-import { AdapterUser } from "next-auth/adapters";+import { AdapterAccount, AdapterUser } from "next-auth/adapters";import { JWT } from "next-auth/jwt";import CredentialsProvider from "next-auth/providers/credentials";import EmailProvider from "next-auth/providers/email";@@ -22,12 +22,12 @@ import { isProduction, shouldApplyRateLimit } from "../api/environment";import { isSamlEnforcedForEmailDomain } from "../api/workspaces/is-saml-enforced-for-email-domain";import { qstash } from "../cron";import { completeProgramApplications } from "../partners/complete-program-applications";-import { FRAMER_API_HOST } from "./constants";import {exceededLoginAttemptsThreshold,incrementLoginAttempts,} from "./lock-account";import { validatePassword } from "./password";+import { SSO_LOGIN_PROGRAMS } from "./sso-login-programs";import { trackDubLead } from "./track-dub-lead";const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;@@ -46,6 +46,25 @@ const CustomPrismaAdapter = (p: PrismaClient) => {},});},+ // Some IdPs (e.g. Beehiiv) return extra token fields+ // so we need to only include the fields that are valid columns on Account table+ linkAccount: (account: AdapterAccount) =>+ p.account.create({+ data: {+ userId: account.userId,+ type: account.type,+ provider: account.provider,+ providerAccountId: account.providerAccountId,+ refresh_token: account.refresh_token,+ refresh_token_expires_in: account.refresh_token_expires_in as any,+ access_token: account.access_token,+ expires_at: account.expires_at,+ token_type: account.token_type,+ scope: account.scope,+ id_token: account.id_token,+ session_state: account.session_state,+ },+ }),};};@@ -299,24 +318,26 @@ export const authOptions: NextAuthOptions = {},}),- // Framer- {- id: "framer",- name: "Framer",- type: "oauth",- clientId: process.env.FRAMER_CLIENT_ID,- clientSecret: process.env.FRAMER_CLIENT_SECRET,- checks: ["state"],+ // SSO Login Programs+ ...SSO_LOGIN_PROGRAMS.map(({ slug, name, oauth, mapProfile }) => ({+ id: slug,+ name,+ type: "oauth" as const,+ clientId: oauth.clientId,+ clientSecret: oauth.clientSecret,+ checks: ["state" as const],authorization: {- url: `${FRAMER_API_HOST}/auth/oauth/authorize`,+ url: oauth.authorizationUrl,params: {- scope: "email",+ scope: oauth.scope,response_type: "code",},},- token: `${FRAMER_API_HOST}/auth/oauth/token`,- userinfo: `${FRAMER_API_HOST}/auth/oauth/profile`,- profile({ sub, email, name, picture }) {+ token: oauth.tokenUrl,+ userinfo: oauth.userInfoUrl,+ profile(profile) {+ if (mapProfile) return mapProfile(profile);+ const { sub, email, name, picture } = profile;return {id: sub,name,@@ -324,7 +345,7 @@ export const authOptions: NextAuthOptions = {image: picture,};},- },+ })),],// @ts-ignoreadapter: CustomPrismaAdapter(prisma),@@ -478,33 +499,6 @@ export const authOptions: NextAuthOptions = {}),]);}- // Login with Framer- } else if (account?.provider === "framer") {- const userFound = await prisma.user.findUnique({- where: {- email: user.email,- },- include: {- accounts: true,- },- });-- // account doesn't exist, let the user sign in- if (!userFound) {- return true;- }-- const otherAccounts = userFound?.accounts.filter(- (account) => account.provider !== "framer",- );-- // we don't allow account linking for Framer partners- // so redirect to the standard login page- if (otherAccounts && otherAccounts.length > 0) {- throw new Error("framer-account-linking-not-allowed");- }-- return true;}return true;},diff --git a/apps/web/lib/auth/sso-login-programs.ts b/apps/web/lib/auth/sso-login-programs.tsnew file mode 100644index 0000000000..fe2935bea4--- /dev/null+++ b/apps/web/lib/auth/sso-login-programs.ts@@ -0,0 +1,41 @@+export const SSO_LOGIN_PROGRAMS = [+ {+ name: "Framer",+ slug: "framer",+ logo: "https://assets.dub.co/companies/framer.svg",+ applyUrl: "https://www.framer.com/creators",+ oauth: {+ clientId: process.env.FRAMER_CLIENT_ID as string,+ clientSecret: process.env.FRAMER_CLIENT_SECRET as string,+ authorizationUrl: "https://api.framer.com/auth/oauth/authorize",+ scope: "email",+ tokenUrl: "https://api.framer.com/auth/oauth/token",+ userInfoUrl: "https://api.framer.com/auth/oauth/profile",+ },+ },+ {+ name: "Beehiiv",+ slug: "beehiiv",+ logo: "https://assets.dub.co/companies/beehiiv.svg",+ applyUrl: "https://partners.dub.co/programs/marketplace/beehiiv",+ oauth: {+ clientId: process.env.BEEHIIV_CLIENT_ID as string,+ clientSecret: process.env.BEEHIIV_CLIENT_SECRET as string,+ authorizationUrl: "https://app.beehiiv.com/oauth/authorize",+ scope: "identify:read",+ tokenUrl: "https://app.beehiiv.com/oauth/token",+ userInfoUrl: "https://api.beehiiv.com/v2/users/identify",+ },+ // Beehiiv doesn't use the standard OpenID Connect userinfo payload+ // @see: https://developers.beehiiv.com/api-reference/oauth-users/identify+ mapProfile: (profile) => {+ const { email, first_name, last_name, profile_picture } = profile;+ return {+ id: email || undefined,+ name: `${first_name} ${last_name}`.trim(),+ email,+ image: profile_picture,+ };+ },+ },+];diff --git a/apps/web/ui/auth/login/login-form.tsx b/apps/web/ui/auth/login/login-form.tsxindex 97250b28ae..f1c18ec085 100644--- a/apps/web/ui/auth/login/login-form.tsx+++ b/apps/web/ui/auth/login/login-form.tsx@@ -35,8 +35,6 @@ export const errorCodes = {"Account has been locked due to too many login attempts. Please contact support to unlock your account.","too-many-login-attempts": "Too many login attempts. Please try again later.","email-not-verified": "Please verify your email address.",- "framer-account-linking-not-allowed":- "It looks like you already have an account with us. Please sign in with your Framer account email instead.","require-saml-sso":"Your organization requires authentication through your company's identity provider.",Callback:@@ -45,6 +43,8 @@ export const errorCodes = {"There was an issue signing you in. Please ensure your provider settings are correct.",OAuthCallback:"We faced a problem while processing the response from the OAuth provider. Please try again.",+ OAuthAccountNotLinked:+ "It looks like you already have an account with this email. Please sign in with your account email instead.",};export const LoginFormContext = createContext<{diff --git a/packages/ui/src/icons/beehiiv.tsx b/packages/ui/src/icons/beehiiv.tsxnew file mode 100644index 0000000000..d89d83fafb--- /dev/null+++ b/packages/ui/src/icons/beehiiv.tsx@@ -0,0 +1,19 @@+import { SVGProps } from "react";++export function Beehiiv(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ width="22"+ height="20"+ fill="none"+ viewBox="0 0 22 20"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <path+ fill="currentColor"+ d="M8.076 20H4.084a2.06 2.06 0 0 1-2.05-2.05v-1.307c0-.418.343-.744.742-.745h5.3zM18.44 15.898c.39 0 .703.304.725.67v1.381A2.06 2.06 0 0 1 17.115 20h-3.993v-4.102zM19.184 10.599c1.125 0 2.032.925 2.032 2.05a2.06 2.06 0 0 1-2.05 2.051h-6.081a2.534 2.534 0 0 0-2.486-2.05c-1.234 0-2.25.889-2.468 2.05H2.05A2.06 2.06 0 0 1 0 12.65a2.06 2.06 0 0 1 2.05-2.051zM16.897 5.3a2.27 2.27 0 0 1 2.268 2.268v1.107a.7.7 0 0 1-.708.726H2.777a.73.73 0 0 1-.726-.726V7.568A2.27 2.27 0 0 1 4.319 5.3zM12.541 0A3.36 3.36 0 0 1 15.9 3.357a.736.736 0 0 1-.745.745H6.08a.747.747 0 0 1-.744-.745A3.36 3.36 0 0 1 8.693 0z"+ ></path>+ </svg>+ );+}diff --git a/packages/ui/src/icons/index.tsx b/packages/ui/src/icons/index.tsxindex 393e558b1d..2274c1c807 100644--- a/packages/ui/src/icons/index.tsx+++ b/packages/ui/src/icons/index.tsx@@ -35,6 +35,7 @@ export * from "./loading-spinner";// brand logosexport * from "./anthropic";+export * from "./beehiiv";export * from "./bing";export * from "./facebook";export * from "./github";