{"record":{"id":"7b1990ab5e4f6d10","repo":"RocketChat/Rocket.Chat","slug":"error-could-not-save-identity","errorCode":"error-could-not-save-identity","errorMessage":"Could not save user identity","messagePattern":"Could not save user identity","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/users/saveUser/saveUser.ts","lineNumber":131,"sourceCode":"\t\toptions?.auditStore?.setOriginalUser(oldUserData);\n\n\t\tawait validateUserEditing(userId, userData);\n\n\t\t// update user\n\t\tconst updater = Users.getUpdater();\n\n\t\tif (userData.hasOwnProperty('username') || userData.hasOwnProperty('name')) {\n\t\t\tif (\n\t\t\t\t!(await saveUserIdentity({\n\t\t\t\t\t_id: userData._id,\n\t\t\t\t\tusername: userData.username,\n\t\t\t\t\tname: userData.name,\n\t\t\t\t\tupdateUsernameInBackground: true,\n\t\t\t\t\tupdater,\n\t\t\t\t\tsession,\n\t\t\t\t}))\n\t\t\t) {\n\t\t\t\tthrow new Meteor.Error('error-could-not-save-identity', 'Could not save user identity', {\n\t\t\t\t\tmethod: 'saveUser',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (typeof userData.statusText === 'string') {\n\t\t\tawait setStatusText(oldUserData, userData.statusText, { updater, session });\n\t\t}\n\n\t\tif (userData.email) {\n\t\t\tconst shouldSendVerificationEmailToUser = userData.verified !== true;\n\t\t\tawait setEmail(userData._id, userData.email, shouldSendVerificationEmailToUser, userData.verified === true, updater);\n\t\t}\n\n\t\tif (\n\t\t\tuserData.password?.trim() &&\n\t\t\t(await hasPermissionAsync(userId, 'edit-other-user-password')) &&\n\t\t\tpasswordPolicy.validate(userData.password)","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/saveUser/saveUser.ts#L113-L149","documentation":"saveUser throws error-could-not-save-identity (method 'saveUser') when saveUserIdentity returns false while applying username/name changes. False means: the target user vanished, the new username failed validateName, or _setUsername could not assign it (unavailable or blocked). saveUser aggregates all these into one error, so the underlying cause must be inferred.","triggerScenarios":"Renaming a user to an already-taken or reserved username through the admin user editor (saveUser with username set); a username with invalid characters; concurrent deletion of the target during the save.","commonSituations":"Admin UI rename colliding with an existing username (including case-insensitive matches); import jobs assigning names that hit the blocked list; identity sync (SAML/OAuth) pushing a name change that fails validation.","solutions":["Pre-check availability with checkUsernameAvailability(newUsername) before calling saveUser","Verify the new username passes the format rules (validateName) and is not in the blocked list","If it still fails, check whether the target user still exists and inspect server logs from _setUsername for the precise refusal"],"exampleFix":"// before\nawait saveUser(actorId, { _id: targetId, username: 'taken.name' });\n\n// after\nconst available = await checkUsernameAvailability('taken.name');\nif (!available) throw new Error('Username already in use');\nawait saveUser(actorId, { _id: targetId, username: 'taken.name' });","handlingStrategy":"validation","validationCode":"if (userData.username) {\n  const available = await checkUsernameAvailability(userData.username);\n  if (!available) throw new Error('Username already in use');\n}\nawait saveUser(actorId, userData);","typeGuard":"const isIdentitySaveError = (e: unknown): boolean =>\n  typeof e === 'object' && e !== null && (e as { error?: unknown }).error === 'error-could-not-save-identity';","tryCatchPattern":"try {\n  await saveUser(actorId, userData);\n} catch (e) {\n  if (isIdentitySaveError(e)) {\n    // aggregated cause: username taken, invalid format, or user deleted — check each in order\n    await diagnoseIdentitySave(userData);\n  }\n}","preventionTips":["Always run checkUsernameAvailability before username-bearing saves","Treat this error as ambiguous; the definitive refusal is logged deeper in _setUsername"],"tags":["users","username","save-user","identity","rename"],"backgroundTag":"username-already-taken","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}