{"record":{"id":"a3db67050f404743","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-a3db67","errorCode":"error-invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/users/checkUsernameAvailability.ts","lineNumber":31,"sourceCode":"\nconst toRegExp = (username: string): RegExp => new RegExp(`^${escapeRegExp(username).trim()}$`, 'i');\n\nsettings.watch('Accounts_BlockedUsernameList', (value: string) => {\n\tusernameBlackList = ['all', 'here'].concat(value.split(',')).map(toRegExp);\n});\n\nconst usernameIsBlocked = (username: string, usernameBlackList: RegExp[]): boolean | number =>\n\tusernameBlackList.length && usernameBlackList.some((restrictedUsername) => restrictedUsername.test(escapeRegExp(username).trim()));\n\nexport const checkUsernameAvailabilityWithValidation = async function (userId: string, username: string): Promise<boolean> {\n\tif (!username) {\n\t\tthrow new Meteor.Error('error-invalid-username', 'Invalid username', { method: 'setUsername' });\n\t}\n\n\tconst user = await Users.findOneById(userId, { projection: { username: 1 } });\n\n\tif (!user) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setUsername' });\n\t}\n\n\tif (user.username && !settings.get('Accounts_AllowUsernameChange')) {\n\t\tthrow new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setUsername' });\n\t}\n\n\tif (user.username === username) {\n\t\treturn true;\n\t}\n\treturn checkUsernameAvailability(username);\n};\n\nexport const checkUsernameAvailability = async function (username: string, type: UsernameAvailabilityCheckType = 'user'): Promise<boolean> {\n\tif (usernameIsBlocked(username, usernameBlackList) || !validateName(username)) {\n\t\tthrow new Meteor.Error('error-blocked-username', `${_.escape(username)} is blocked and can't be used!`, {\n\t\t\tmethod: 'checkUsernameAvailability',\n\t\t\tfield: username,\n\t\t});","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/checkUsernameAvailability.ts#L13-L49","documentation":"checkUsernameAvailabilityWithValidation throws error-invalid-user (method 'setUsername') when Users.findOneById(userId) returns null: the session/acting user no longer exists in the database. The user record was deleted or the id is bogus, so availability cannot be validated on their behalf.","triggerScenarios":"A logged-in user's account is deleted while their session is still active and they attempt to set a username; server-side code calling the validation with a made-up or stale user id.","commonSituations":"Admin deletes a user whose browser tab remains open; token reuse after account removal; test code passing hard-coded ids that do not exist in the current database.","solutions":["Verify the user exists (Users.findOneById or GET /api/v1/users.info) before initiating the username flow","If this fires for a live session, log the user out: their account is gone and every subsequent call will fail","In tests, seed the user record before invoking setUsername"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const me = await Users.findOneById(userId, { projections: { _id: 1 } });\nif (!me) throw new Error('Session user no longer exists');\nawait checkUsernameAvailabilityWithValidation(userId, username);","typeGuard":"const isMeteorErrorCode = (e: unknown, code: string): e is { error: string; reason?: string } =>\n  typeof e === 'object' && e !== null && (e as { error?: unknown }).error === code;","tryCatchPattern":"try {\n  await checkUsernameAvailabilityWithValidation(userId, username);\n} catch (e) {\n  if (isMeteorErrorCode(e, 'error-invalid-user')) {\n  \tforceLogout(); // account deleted, session is dead\n  }\n}","preventionTips":["Handle account-deleted sessions globally (401/invalid-user) with a logout redirect","Seed acting users in tests before calling user-scoped methods"],"tags":["users","username","session","lookup"],"backgroundTag":"user-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}