fix: signup username collision (#25435)

* fix: signup username collision

* tests: add unit tests
This commit is contained in:
Udit Takkar
2025-11-27 15:41:38 +00:00
committed by GitHub
parent a23400777a
commit 0f84cedad2
2 changed files with 26 additions and 2 deletions
@@ -15,8 +15,8 @@ import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService"
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
import ImpersonationProvider from "@calcom/features/ee/impersonation/lib/ImpersonationProvider";
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
import { clientSecretVerifier, hostedCal, isSAMLLoginEnabled } from "@calcom/features/ee/sso/lib/saml";
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
@@ -962,7 +962,7 @@ export const getOptions = ({
email: user.email,
// Slugify the incoming name and append a few random characters to
// prevent conflicts for users with the same name.
username: getOrgUsernameFromEmail(user.name, getDomainFromEmail(user.email)),
username: getOrgUsernameFromEmail(user.email, getDomainFromEmail(user.email)),
emailVerified: new Date(Date.now()),
name: user.name,
identityProvider: idP,
@@ -16,6 +16,30 @@ describe("getOrgUsernameFromEmail", () => {
const result = getOrgUsernameFromEmail(email, autoAcceptEmailDomain);
expect(result).toBe("john.doe-example");
});
it("should generate unique usernames for different emails even with same name", () => {
const email1 = "alice@acme.com";
const email2 = "alice+work@corp.com";
const username1 = getOrgUsernameFromEmail(email1, null);
const username2 = getOrgUsernameFromEmail(email2, null);
expect(username1).toBe("alice-acme");
expect(username2).toBe("alice-work-corp");
expect(username1).not.toBe(username2);
});
it("should handle email with plus sign correctly", () => {
const email = "bob+test@example.com";
const result = getOrgUsernameFromEmail(email, "example.com");
expect(result).toBe("bob-test");
});
it("should handle null autoAcceptEmailDomain", () => {
const email = "user@company.com";
const result = getOrgUsernameFromEmail(email, null);
expect(result).toBe("user-company");
});
});
describe("deriveNameFromOrgUsername", () => {