Skip to content

Commit 0f84ced

Browse files
authored
fix: signup username collision (calcom#25435)
* fix: signup username collision * tests: add unit tests
1 parent a234007 commit 0f84ced

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

packages/features/auth/lib/next-auth-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService"
1515
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
1616
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
1717
import ImpersonationProvider from "@calcom/features/ee/impersonation/lib/ImpersonationProvider";
18-
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
1918
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
19+
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
2020
import { clientSecretVerifier, hostedCal, isSAMLLoginEnabled } from "@calcom/features/ee/sso/lib/saml";
2121
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
2222
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
@@ -962,7 +962,7 @@ export const getOptions = ({
962962
email: user.email,
963963
// Slugify the incoming name and append a few random characters to
964964
// prevent conflicts for users with the same name.
965-
username: getOrgUsernameFromEmail(user.name, getDomainFromEmail(user.email)),
965+
username: getOrgUsernameFromEmail(user.email, getDomainFromEmail(user.email)),
966966
emailVerified: new Date(Date.now()),
967967
name: user.name,
968968
identityProvider: idP,

packages/features/auth/signup/utils/getOrgUsernameFromEmail.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ describe("getOrgUsernameFromEmail", () => {
1616
const result = getOrgUsernameFromEmail(email, autoAcceptEmailDomain);
1717
expect(result).toBe("john.doe-example");
1818
});
19+
20+
it("should generate unique usernames for different emails even with same name", () => {
21+
const email1 = "alice@acme.com";
22+
const email2 = "alice+work@corp.com";
23+
24+
const username1 = getOrgUsernameFromEmail(email1, null);
25+
const username2 = getOrgUsernameFromEmail(email2, null);
26+
27+
expect(username1).toBe("alice-acme");
28+
expect(username2).toBe("alice-work-corp");
29+
expect(username1).not.toBe(username2);
30+
});
31+
32+
it("should handle email with plus sign correctly", () => {
33+
const email = "bob+test@example.com";
34+
const result = getOrgUsernameFromEmail(email, "example.com");
35+
expect(result).toBe("bob-test");
36+
});
37+
38+
it("should handle null autoAcceptEmailDomain", () => {
39+
const email = "user@company.com";
40+
const result = getOrgUsernameFromEmail(email, null);
41+
expect(result).toBe("user-company");
42+
});
1943
});
2044

2145
describe("deriveNameFromOrgUsername", () => {

0 commit comments

Comments
 (0)