fix: allow organizations to sign up with existing usernames (#25941)
* fix: allow organizations to sign up with existing usernames - Update usernameCheck to check organization context when currentOrgDomain is provided - Organizations can now use usernames that exist in other orgs or global namespace - Only checks username availability within the specific organization - Fixes issue where org signups were blocked by global username conflicts Refs #25800 * Update username.ts
This commit is contained in:
1 parent
a1a5f2477d
commit
47a5fc7bf7
1 file changed
+31
-3
@@ -1,5 +1,7 @@
|
|||||||
import type { NextResponse } from "next/server";
|
import type { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
import { ErrorCode } from "@calcom/lib/errorCodes";
|
||||||
|
import { ErrorWithCode } from "@calcom/lib/errors";
|
||||||
import slugify from "@calcom/lib/slugify";
|
import slugify from "@calcom/lib/slugify";
|
||||||
import prisma from "@calcom/prisma";
|
import prisma from "@calcom/prisma";
|
||||||
import { RedirectType } from "@calcom/prisma/enums";
|
import { RedirectType } from "@calcom/prisma/enums";
|
||||||
@@ -116,11 +118,36 @@ const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | nu
|
|||||||
|
|
||||||
const username = slugify(usernameRaw);
|
const username = slugify(usernameRaw);
|
||||||
|
|
||||||
|
let organizationId: number | null = null;
|
||||||
|
if (currentOrgDomain) {
|
||||||
|
// Get organizationId from orgSlug
|
||||||
|
const organization = await prisma.team.findFirst({
|
||||||
|
where: {
|
||||||
|
isOrganization: true,
|
||||||
|
OR: [
|
||||||
|
{ slug: currentOrgDomain },
|
||||||
|
{ metadata: { path: ["requestedSlug"], equals: currentOrgDomain } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!organization) {
|
||||||
|
throw new ErrorWithCode(
|
||||||
|
ErrorCode.NotFound,
|
||||||
|
`Organization with domain "${currentOrgDomain}" not found`,
|
||||||
|
{ currentOrgDomain }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
organizationId = organization.id;
|
||||||
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findFirst({
|
const user = await prisma.user.findFirst({
|
||||||
where: {
|
where: {
|
||||||
username,
|
username,
|
||||||
// Simply remove it when we drop organizationId column
|
// Check in the specific organization context, or global namespace if no org
|
||||||
organizationId: null,
|
organizationId: organizationId ?? null,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -140,12 +167,13 @@ const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | nu
|
|||||||
response.premium = true;
|
response.premium = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get list of similar usernames in the db
|
// get list of similar usernames in the db (scoped to organization if checking in org context)
|
||||||
const users = await prisma.user.findMany({
|
const users = await prisma.user.findMany({
|
||||||
where: {
|
where: {
|
||||||
username: {
|
username: {
|
||||||
contains: username,
|
contains: username,
|
||||||
},
|
},
|
||||||
|
...(organizationId !== null ? { organizationId } : { organizationId: null }),
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
username: true,
|
username: true,
|
||||||
|
|||||||
Reference in new issue
Block a user