{"record":{"id":"6755a9328da4edb9","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-6755a9","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/setUsername.ts","lineNumber":39,"sourceCode":"import { addUserToRoom } from '../rooms/addUserToRoom';\nimport { joinDefaultChannels } from '../rooms/joinDefaultChannels';\n\nconst isUserInFederatedRooms = async (userId: string): Promise<boolean> => {\n\tconst cursor = Subscriptions.findUserFederatedRoomIds(userId);\n\tconst hasAny = await cursor.hasNext();\n\tawait cursor.close();\n\treturn hasAny;\n};\n\nexport const setUsernameWithValidation = async (userId: string, username: string, joinDefaultChannelsSilenced?: boolean): Promise<void> => {\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);\n\n\tif (!user) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setUsername' });\n\t}\n\n\tif (isUserNativeFederated(user) || (await isUserInFederatedRooms(userId))) {\n\t\tthrow new Meteor.Error('error-not-allowed', 'Cannot change username for federated users or users in federated rooms', {\n\t\t\tmethod: 'setUsername',\n\t\t});\n\t}\n\n\tif (user.username && !settings.get('Accounts_AllowUsernameChange')) {\n\t\tthrow new Meteor.Error('error-not-allowed', 'Not allowed');\n\t}\n\n\tif (user.username === username || (user.username && user.username.toLowerCase() === username.toLowerCase())) {\n\t\treturn;\n\t}\n\n\tif (!validateUsername(username)) {\n\t\tthrow new Meteor.Error(","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/setUsername.ts#L21-L57","documentation":"Thrown by setUsernameWithValidation after Users.findOneById(userId) returns null: the userId passed to the method does not match any user document. It is the first precondition of the rename flow (setUsername.ts:36-40) and fires before the federation, permission, and availability checks. The Meteor.Error code is 'error-invalid-user' with { method: 'setUsername' } in the details.","triggerScenarios":"Calling the setUsername flow with an _id that was deleted, never existed, or is actually a username string passed by mistake; also a race where the account is deleted between client load and server execution.","commonSituations":"Stale user references in client state after account deletion; test fixtures that invoke the method without creating the user; passing user.username where user._id is expected; imported/migrated data containing dangling user ids.","solutions":["Confirm you are passing the user's _id (not the username) to setUsernameWithValidation","Pre-check the user exists with Users.findOneById and surface a 'user not found' state instead of letting the method throw","If the id arrives from the client, re-resolve it server-side from the authenticated user (Meteor.userId()) before using it"],"exampleFix":"// before\nawait setUsernameWithValidation(userId, 'new.name'); // throws error-invalid-user when userId is stale\n\n// after\nconst user = await Users.findOneById(userId, { projection: { _id: 1 } });\nif (!user) {\n  throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setUsername' });\n}\nawait setUsernameWithValidation(userId, 'new.name');","handlingStrategy":"validation","validationCode":"import { Users } from '@rocket.chat/models';\n\nconst user = await Users.findOneById(userId, { projection: { _id: 1 } });\nif (!user) {\n  // do not call setUsernameWithValidation; refresh client state instead\n}","typeGuard":"const isUserId = (v: unknown): v is string => typeof v === 'string' && v.trim().length > 0;","tryCatchPattern":"try {\n  await setUsernameWithValidation(userId, username);\n} catch (error) {\n  if (error instanceof Meteor.Error && error.error === 'error-invalid-user') {\n    // user vanished mid-flight: reload the session/user data\n  } else {\n    throw error;\n  }\n}","preventionTips":["Derive userId from the authenticated connection (Meteor.userId()) instead of trusting a client-supplied parameter","Create the user fixture before calling the method in tests","Treat a null return from Users.findOneById as terminal — never fall through to the rename call"],"tags":["meteor","users","username","not-found"],"backgroundTag":"user-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}