diff --git a/core/api/src/services/mongoose/accounts.ts b/core/api/src/services/mongoose/accounts.ts index c310b86f8c..c8a485d3e1 100644 --- a/core/api/src/services/mongoose/accounts.ts +++ b/core/api/src/services/mongoose/accounts.ts @@ -1,4 +1,4 @@ -import { caseInsensitiveRegex, parseRepositoryError } from "./utils" +import { parseRepositoryError } from "./utils" import { AccountStatus } from "@/domain/accounts" import { @@ -44,7 +44,9 @@ export const AccountsRepository = (): IAccountsRepository => { username: Username, ): Promise => { try { - const result = await Account.findOne({ username: caseInsensitiveRegex(username) }) + const result = await Account.findOne({ username }) + .collation({ locale: "en", strength: 2 }) + .hint({ username: 1 }) if (!result) { return new CouldNotFindAccountFromUsernameError(username) } diff --git a/core/api/src/services/mongoose/schema.ts b/core/api/src/services/mongoose/schema.ts index b9bec71721..030c40816a 100644 --- a/core/api/src/services/mongoose/schema.ts +++ b/core/api/src/services/mongoose/schema.ts @@ -202,13 +202,18 @@ const AccountSchema = new Schema( username: { type: String, - match: [UsernameRegex, "Username can only have alphabets, numbers and underscores"], + match: [ + UsernameRegex, + "Username can only have alphabets, numbers, and underscores", + ], minlength: 3, maxlength: 50, index: { unique: true, collation: { locale: "en", strength: 2 }, - partialFilterExpression: { username: { $type: "string" } }, + partialFilterExpression: { + username: { $type: "string", $exists: true }, + }, }, }, contactEnabled: {