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.

Add SAML enforcement check to account existence action

Kiran Kc79393dbdf17
diff --git a/apps/web/lib/actions/check-account-exists.ts b/apps/web/lib/actions/check-account-exists.ts
index 953f097c4b..7f458d4f21 100644
--- a/apps/web/lib/actions/check-account-exists.ts
+++ b/apps/web/lib/actions/check-account-exists.ts
@@ -2,7 +2,9 @@
import { ratelimit } from "@/lib/upstash";
import { prisma } from "@dub/prisma";
+import { APP_HOSTNAMES } from "@dub/utils";
import { getIP } from "../api/utils";
+import { isGenericEmail } from "../emails";
import z from "../zod";
import { emailSchema } from "../zod/schemas/auth";
import { throwIfAuthenticated } from "./auth/throw-if-authenticated";
@@ -27,17 +29,46 @@ export const checkAccountExistsAction = actionClient
throw new Error("Too many requests. Please try again later.");
}
- const user = await prisma.user.findUnique({
- where: {
- email,
- },
- select: {
- passwordHash: true,
- },
- });
+ // Check SAML enforcement
+ const hostname = new URL(process.env.NEXTAUTH_URL as string).hostname;
+ const emailDomain = email.split("@")[1];
+ const shouldCheckSAML = APP_HOSTNAMES.has(hostname) && !isGenericEmail(emailDomain);
+
+ // Run both queries in parallel
+ const [user, workspace] = await Promise.all([
+ // Find the user
+ prisma.user.findUnique({
+ where: {
+ email,
+ },
+ select: {
+ passwordHash: true,
+ },
+ }),
+ // Check SAML enforcement (only if needed)
+ shouldCheckSAML
+ ? prisma.project.findUnique({
+ where: {
+ ssoEmailDomain: emailDomain,
+ },
+ select: {
+ ssoEnforcedAt: true,
+ },
+ })
+ : Promise.resolve(null),
+ ]);
+
+ if (workspace?.ssoEnforcedAt) {
+ return {
+ accountExists: !!user,
+ hasPassword: !!user?.passwordHash,
+ requireSAML: true,
+ };
+ }
return {
accountExists: !!user,
hasPassword: !!user?.passwordHash,
+ requireSAML: false,
};
});
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index a707ec8080..53896e27ea 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -7,7 +7,7 @@ import { sendEmail } from "@dub/email";
import LoginLink from "@dub/email/templates/login-link";
import { prisma } from "@dub/prisma";
import { PrismaClient } from "@dub/prisma/client";
-import { APP_DOMAIN_WITH_NGROK, PARTNERS_HOSTNAMES } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, APP_HOSTNAMES } from "@dub/utils";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { waitUntil } from "@vercel/functions";
import { User, type NextAuthOptions } from "next-auth";
@@ -592,7 +592,7 @@ export const authOptions: NextAuthOptions = {
// Checks if SAML SSO is enforced for a given email domain
export const isSamlEnforcedForDomain = async (email: string) => {
const hostname = new URL(process.env.NEXTAUTH_URL as string).hostname;
- if (PARTNERS_HOSTNAMES.has(hostname)) {
+ if (!APP_HOSTNAMES.has(hostname)) {
return;
}
diff --git a/apps/web/ui/auth/login/email-sign-in.tsx b/apps/web/ui/auth/login/email-sign-in.tsx
index d688888029..ad566ea511 100644
--- a/apps/web/ui/auth/login/email-sign-in.tsx
+++ b/apps/web/ui/auth/login/email-sign-in.tsx
@@ -48,7 +48,15 @@ export const EmailSignIn = ({ next }: { next?: string }) => {
return;
}
- const { accountExists, hasPassword } = result.data;
+ const { accountExists, hasPassword, requireSAML } = result.data;
+
+ if (requireSAML) {
+ setClickedMethod(undefined);
+ toast.error(
+ "Your organization requires authentication through your company's identity provider.",
+ );
+ return;
+ }
if (accountExists && hasPassword) {
setShowPasswordField(true);