🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(cloud): 2-step sign in to make enterprise sso easier (#7250)
Marc Klingen85130cecb379
diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsxindex a206c7a6c4..8338755c99 100644--- a/web/src/pages/auth/sign-in.tsx+++ b/web/src/pages/auth/sign-in.tsx@@ -28,7 +28,7 @@ import { CloudRegionSwitch } from "@/src/features/auth/components/AuthCloudRegioimport { PasswordInput } from "@/src/components/ui/password-input";import { Turnstile } from "@marsidev/react-turnstile";import { isAnySsoConfigured } from "@/src/ee/features/multi-tenant-sso/utils";-import { Shield, Code } from "lucide-react";+import { Code } from "lucide-react";import { useRouter } from "next/router";import { captureException } from "@sentry/nextjs";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";@@ -419,7 +419,12 @@ export default function SignIn({const [credentialsFormError, setCredentialsFormError] = useState<string | null>(nextAuthErrorDescription ?? nextAuthError);- const [ssoLoading, setSsoLoading] = useState<boolean>(false);+ // Two-step login flow: ask for email first, detect SSO, then either redirect to SSO or reveal password field.+ // Skip this flow when no SSO is configured - show password field immediately+ const [showPasswordStep, setShowPasswordStep] = useState<boolean>(+ !authProviders.sso,+ );+ const [continueLoading, setContinueLoading] = useState<boolean>(false);const capture = usePostHogClientCapture();const [turnstileToken, setTurnstileToken] = useState<string>();@@ -477,38 +482,72 @@ export default function SignIn({}}- async function handleSsoSignIn() {- setSsoLoading(true);+ /**+ * First-step handler ("Continue" button).+ * 1. Validates email.+ * 2. Queries backend to see if a tenant-specific SSO provider is configured.+ * ‑ If found: redirects to that provider immediately.+ * ‑ Otherwise: reveals password input so the user can finish with credentials.+ * 3. Gracefully handles network errors and edge cases.+ */+ async function handleContinue() {+ setContinueLoading(true);setCredentialsFormError(null);credentialsForm.clearErrors();- // get current email field, verify it, add input error if not valid++ // Ensure email is valid before hitting the APIconst emailSchema = z.string().email();const email = emailSchema.safeParse(credentialsForm.getValues("email"));if (!email.success) {credentialsForm.setError("email", {message: "Invalid email address",});- setSsoLoading(false);+ setContinueLoading(false);return;}- // current email domain++ // Extract domain and check whether SSO is configured for itconst domain = email.data.split("@")[1]?.toLowerCase();- const res = await fetch(- `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/auth/check-sso`,- {- method: "POST",- headers: { "Content-Type": "application/json" },- body: JSON.stringify({ domain }),- },- );- if (!res.ok) {- setCredentialsFormError("SSO is not enabled for this domain.");- setSsoLoading(false);- } else {- const { providerId } = await res.json();- capture("sign_in:button_click", { provider: "sso" });- void signIn(providerId);+ try {+ const res = await fetch(+ `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/auth/check-sso`,+ {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ domain }),+ },+ );++ if (res.ok) {+ // Enterprise SSO found – redirect straight away+ const { providerId } = await res.json();+ capture("sign_in:button_click", { provider: "sso_auto" });+ void signIn(providerId);+ return; // stop further execution – page redirect expected+ }++ // No SSO – fall back to password step+ setShowPasswordStep(true);++ // Auto-focus password input when password step becomes visible+ setTimeout(() => {+ // Find and focus the password input+ // Ref did not work, so we use a more specific selector+ const passwordInput = document.querySelector(+ 'input[name="password"]',+ ) as HTMLInputElement;+ if (passwordInput) {+ passwordInput.focus();+ }+ }, 100);+ } catch (error) {+ console.error(error);+ setCredentialsFormError(+ "Unable to check SSO configuration. Please try again.",+ );+ } finally {+ setContinueLoading(false);}}@@ -543,13 +582,22 @@ export default function SignIn({<div className="mt-14 bg-background px-6 py-10 shadow sm:mx-auto sm:w-full sm:max-w-[480px] sm:rounded-lg sm:px-10"><div className="space-y-6">- {authProviders.credentials ? (+ {/* Email / (optional) password form – only when credentials auth is enabled */}+ {authProviders.credentials && (<Form {...credentialsForm}><formclassName="space-y-6"// eslint-disable-next-line @typescript-eslint/no-misused-promises- onSubmit={credentialsForm.handleSubmit(onCredentialsSubmit)}+ onSubmit={+ showPasswordStep+ ? credentialsForm.handleSubmit(onCredentialsSubmit)+ : (e) => {+ e.preventDefault();+ void handleContinue();+ }+ }>+ {/* Email input – always visible */}<FormFieldcontrol={credentialsForm.control}name="email"@@ -563,78 +611,57 @@ export default function SignIn({</FormItem>)}/>- <FormField- control={credentialsForm.control}- name="password"- render={({ field }) => (- <FormItem>- <FormLabel>- Password{" "}- <Link- href="/auth/reset-password"- className="ml-1 text-xs text-primary-accent hover:text-hover-primary-accent"- tabIndex={-1}- title="What is this?"- >- (forgot password?)- </Link>- </FormLabel>- <FormControl>- <PasswordInput {...field} />- </FormControl>- <FormMessage />- </FormItem>- )}- />++ {/* Password only shown once we know SSO is not configured */}+ {showPasswordStep && (+ <FormField+ control={credentialsForm.control}+ name="password"+ render={({ field }) => (+ <FormItem>+ <FormLabel>+ Password{" "}+ <Link+ href="/auth/reset-password"+ className="ml-1 text-xs text-primary-accent hover:text-hover-primary-accent"+ tabIndex={-1}+ title="What is this?"+ >+ (forgot password?)+ </Link>+ </FormLabel>+ <FormControl>+ <PasswordInput {...field} />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ )}++ {/* Primary action button */}<Button- // this hidden button is needed to submit form by pressing entertype="submit"- className="hidden"- disabled={- env.NEXT_PUBLIC_TURNSTILE_SITE_KEY !== undefined &&- turnstileToken === undefined- }- >- Sign in- </Button>- </form>- </Form>- ) : null}- {(authProviders.credentials || authProviders.sso) && (- <div className="flex flex-row gap-3">- {authProviders.credentials && (- <ButtonclassName="w-full"- loading={credentialsForm.formState.isSubmitting}+ loading={+ showPasswordStep+ ? credentialsForm.formState.isSubmitting+ : continueLoading+ }disabled={(env.NEXT_PUBLIC_TURNSTILE_SITE_KEY !== undefined &&+ showPasswordStep &&turnstileToken === undefined) ||credentialsForm.watch("email") === "" ||- credentialsForm.watch("password") === ""+ (showPasswordStep &&+ credentialsForm.watch("password") === "")}- onClick={credentialsForm.handleSubmit(onCredentialsSubmit)}data-testid="submit-email-password-sign-in-form">- Sign in+ {showPasswordStep ? "Sign in" : "Continue"}</Button>- )}- {authProviders.sso && (- <Button- className="w-full"- variant="secondary"- loading={ssoLoading}- disabled={- (env.NEXT_PUBLIC_TURNSTILE_SITE_KEY !== undefined &&- turnstileToken === undefined) ||- credentialsForm.watch("email") === ""- }- onClick={handleSsoSignIn}- >- <Shield className="mr-3" size={18} />- SSO- </Button>- )}- </div>+ </form>+ </Form>)}{credentialsFormError ? (<div className="text-center text-sm font-medium text-destructive">