fix: add isCompanyEmail check to organization upgrade path (#27813)

Users with personal emails (e.g. Gmail) could enter the org creation flow
at /settings/organizations/new but only hit the company email error at
checkout, with no way to fix it from the form. After changing their email
in settings, the cached store email was still used.

- Add server-side isCompanyEmail check in page.tsx to redirect early
- Add client-side check in CreateANewOrganizationForm with clear messaging
- Always use current session email for non-admin users (not cached store)
- Add tests for isCompanyEmail utility

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
sean-brydonandDevin AI authored and GitHub committed 2026-02-10 06:15:28 +00:00
1 parent b03c00aded
commit 9424649d6f
4 files changed
+171 -16

No files matched your search

@@ -1,12 +1,7 @@
"use client";
import type { SessionContextValue } from "next-auth/react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
import { isCompanyEmail } from "@calcom/features/ee/organizations/lib/utils";
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import slugify from "@calcom/lib/slugify";
@@ -16,13 +11,14 @@ import type { Ensure } from "@calcom/types/utils";
import classNames from "@calcom/ui/classNames";
import { Alert } from "@calcom/ui/components/alert";
import { Button } from "@calcom/ui/components/button";
import { ToggleGroup } from "@calcom/ui/components/form";
import { Form } from "@calcom/ui/components/form";
import { Label } from "@calcom/ui/components/form";
import { TextField } from "@calcom/ui/components/form";
import { Form, Label, TextField, ToggleGroup } from "@calcom/ui/components/form";
import { RadioAreaGroup as RadioArea } from "@calcom/ui/components/radio";
import { useOnboarding } from "@calcom/web/modules/ee/organizations/lib/onboardingStore";
import { useRouter } from "next/navigation";
import type { SessionContextValue } from "next-auth/react";
import { useSession } from "next-auth/react";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
function extractDomainFromEmail(email: string) {
const match = email.match(/^(?:.*?:\/\/)?.*?([\w-]*(?:\.\w{2,}|\.\w{2,}\.\w{2}))(?:[/?#:]|$)/);
@@ -52,6 +48,11 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
const { slug, name, orgOwnerEmail, billingPeriod, pricePerSeat, seats, onboardingId, reset } =
useOnboardingStore();
// For non-admin users, always use the current session email to prevent stale cached email issues.
// This handles the case where a user changes their email in settings and returns to org creation.
const effectiveOrgOwnerEmail = !isAdmin ? defaultOrgOwnerEmail : orgOwnerEmail || defaultOrgOwnerEmail;
const userHasCompanyEmail = isAdmin || isCompanyEmail(effectiveOrgOwnerEmail);
const newOrganizationFormMethods = useForm<{
name: string;
seats: number | null;
@@ -62,9 +63,9 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
}>({
defaultValues: {
billingPeriod: billingPeriod ?? BillingPeriod.MONTHLY,
slug: slug ?? (!isAdmin ? deriveSlugFromEmail(defaultOrgOwnerEmail) : undefined),
orgOwnerEmail: orgOwnerEmail || defaultOrgOwnerEmail,
name: name ?? (!isAdmin ? deriveOrgNameFromEmail(defaultOrgOwnerEmail) : undefined),
slug: slug ?? (!isAdmin ? deriveSlugFromEmail(effectiveOrgOwnerEmail) : undefined),
orgOwnerEmail: effectiveOrgOwnerEmail,
name: name ?? (!isAdmin ? deriveOrgNameFromEmail(effectiveOrgOwnerEmail) : undefined),
seats: seats ?? null,
pricePerSeat: pricePerSeat ?? null,
},
@@ -111,6 +112,24 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
});
const needToCreateOnboarding = !onboardingId;
if (!userHasCompanyEmail) {
return (
<div className="stack-y-5">
<Alert
severity="warning"
title={t("use_company_email_to_create_an_organization")}
message={t("update_email_organization_description")}
actions={
<Button href="/settings/my-account/profile" color="secondary" className="mt-2">
{t("update_email_address")}
</Button>
}
/>
</div>
);
}
return (
<>
<Form