summary refs log tree commit diff
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2023-09-08 22:38:56 -0400
committerPuyodead1 <puyodead@protonmail.com>2023-12-23 16:38:16 -0500
commitd88e9273ffcc95477e560e7b1a3bd32011e24f6c (patch)
tree62d130f36ddf96ad980bac2f1a9d2a17154d65d3
parentadd unique-username routes to unauthenticated list (diff)
downloadserver-d88e9273ffcc95477e560e7b1a3bd32011e24f6c.tar.xz
fix being able to register with taken username
-rw-r--r--src/api/routes/experiments.ts2
-rw-r--r--src/api/routes/unique-username/username-attempt-unauthed.ts2
-rw-r--r--src/api/routes/users/@me/index.ts2
-rw-r--r--src/util/entities/User.ts13
4 files changed, 11 insertions, 8 deletions
diff --git a/src/api/routes/experiments.ts b/src/api/routes/experiments.ts

index 0cad7342..24fd3ef1 100644 --- a/src/api/routes/experiments.ts +++ b/src/api/routes/experiments.ts
@@ -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); }); diff --git a/src/api/routes/unique-username/username-attempt-unauthed.ts b/src/api/routes/unique-username/username-attempt-unauthed.ts
index a1f63a69..3af1fa88 100644 --- a/src/api/routes/unique-username/username-attempt-unauthed.ts +++ b/src/api/routes/unique-username/username-attempt-unauthed.ts
@@ -25,7 +25,7 @@ router.post( } res.json({ - taken: !User.isUsernameAvailable(body.username), + taken: !(await User.isUsernameAvailable(body.username)), }); }, ); diff --git a/src/api/routes/users/@me/index.ts b/src/api/routes/users/@me/index.ts
index 55d2ce12..5c3cfb29 100644 --- a/src/api/routes/users/@me/index.ts +++ b/src/api/routes/users/@me/index.ts
@@ -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", diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts
index 5ec9862e..07b68b6d 100644 --- a/src/util/entities/User.ts +++ b/src/util/entities/User.ts
@@ -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", @@ -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; } }