Skip to content

Commit 6a16366

Browse files
authored
fix: Username check for sign-up with invitation in org context (#10375)
1 parent 5a9ee20 commit 6a16366

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

apps/web/pages/api/auth/signup.ts

+30-31
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
4242
return;
4343
}
4444

45-
// There is an existingUser if the username matches
46-
// OR if the email matches AND either the email is verified
47-
// or both username and password are set
48-
const existingUser = await prisma.user.findFirst({
49-
where: {
50-
OR: [
51-
{ username },
52-
{
53-
AND: [
54-
{ email: userEmail },
55-
{
56-
OR: [
57-
{ emailVerified: { not: null } },
58-
{
59-
AND: [{ password: { not: null } }, { username: { not: null } }],
60-
},
61-
],
62-
},
63-
],
64-
},
65-
],
66-
},
67-
});
68-
69-
if (existingUser) {
70-
const message: string =
71-
existingUser.email !== userEmail ? "Username already taken" : "Email address is already registered";
72-
73-
return res.status(409).json({ message });
74-
}
75-
7645
let foundToken: { id: number; teamId: number | null; expires: Date } | null = null;
7746
if (token) {
7847
foundToken = await prisma.verificationToken.findFirst({
@@ -100,6 +69,36 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
10069
return res.status(409).json({ message: "Username already taken" });
10170
}
10271
}
72+
} else {
73+
// There is an existingUser if the username matches
74+
// OR if the email matches AND either the email is verified
75+
// or both username and password are set
76+
const existingUser = await prisma.user.findFirst({
77+
where: {
78+
OR: [
79+
{ username },
80+
{
81+
AND: [
82+
{ email: userEmail },
83+
{
84+
OR: [
85+
{ emailVerified: { not: null } },
86+
{
87+
AND: [{ password: { not: null } }, { username: { not: null } }],
88+
},
89+
],
90+
},
91+
],
92+
},
93+
],
94+
},
95+
});
96+
if (existingUser) {
97+
const message: string =
98+
existingUser.email !== userEmail ? "Username already taken" : "Email address is already registered";
99+
100+
return res.status(409).json({ message });
101+
}
103102
}
104103

105104
const hashedPassword = await hashPassword(password);

packages/lib/validateUsernameInOrg.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ export const validateUsernameInOrg = async (usernameSlug: string, teamId: number
2121
},
2222
});
2323

24+
const usersFound = await prisma.user.findMany({
25+
where: {
26+
organizationId: teamId,
27+
},
28+
select: {
29+
username: true,
30+
},
31+
});
32+
33+
takenSlugs = usersFound.map((user) => user.username);
34+
2435
// If only one team is found and it has a parent, then it's an child team
2536
// and we can use the parent id to find all the teams that belong to this org
2637
if (teamsFound && teamsFound.length === 1 && teamsFound[0].parentId) {
@@ -34,9 +45,9 @@ export const validateUsernameInOrg = async (usernameSlug: string, teamId: number
3445
slug: true,
3546
},
3647
});
37-
takenSlugs = childTeams.map((team) => team.slug);
48+
takenSlugs = takenSlugs.concat(childTeams.map((team) => team.slug));
3849
} else {
39-
takenSlugs = teamsFound.map((team) => team.slug);
50+
takenSlugs = takenSlugs.concat(teamsFound.map((team) => team.slug));
4051
}
4152

4253
return !takenSlugs.includes(slugify(usernameSlug));

0 commit comments

Comments
 (0)