Skip to content

Commit

Permalink
fix being able to register with taken username
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Dec 23, 2023
1 parent 56c283d commit d88e927
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/routes/experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ router.get("/", route({}), (req: Request, res: Response) => {
if (uniqueUsernames) {
// hash, revision, bucket, override, population, hash_result, as_mode
// bucket 4 is used by the official client, and enables live checking and suggestions, 3 is only live checking
data.assignments.push([2476969328, 0, 3, -1, 0, 9267, 0, 0]);
data.assignments.push([2476969328, 0, 4, -1, 0, 9267, 0, 0]);
}
res.send(data);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ router.post(
}

res.json({
taken: !User.isUsernameAvailable(body.username),
taken: !(await User.isUsernameAvailable(body.username)),
});
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/users/@me/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ router.patch(
}

// check if username is already taken (pomelo only)
if (!User.isUsernameAvailable(body.username))
if (!(await User.isUsernameAvailable(body.username)))
throw FieldErrors({
username: {
code: "USERNAME_ALREADY_TAKEN",
Expand Down
13 changes: 8 additions & 5 deletions src/util/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export class User extends BaseClass {

if (uniqueUsernames) {
// check if there is already an account with this username
if (!User.isUsernameAvailable(username))
if (!(await User.isUsernameAvailable(username)))
throw FieldErrors({
username: {
code: "USERNAME_ALREADY_TAKEN",
Expand Down Expand Up @@ -463,11 +463,14 @@ export class User extends BaseClass {
}

static async isUsernameAvailable(username: string) {
const user = await User.findOne({
where: { username },
select: ["id"],
// TODO: implement regex check?
const count = await User.count({
where: {
username: username.toLowerCase(),
},
});
return !user;

return count === 0;
}
}

Expand Down

0 comments on commit d88e927

Please sign in to comment.