RocketChat/Rocket.Chat · error · Meteor.Error

error-invalid-user

error-invalid-user

Error message

Invalid user

What it means

The /archive slash command resolves the executing user with Users.findOneById(userId, { username, name }) and requires isRegisterUser - which per core-typings means the user document must have both username and name defined. A missing user record (userId no longer in the users collection) or an incomplete user doc (no name or no username) fails with 'error-invalid-user' before any room logic runs.

Source

Thrown at apps/meteor/server/slashcommands/archiveroom/server.ts:38

		let room;

		if (channel === '') {
			room = await Rooms.findOneById(message.rid);
			if (room?.name) {
				channel = room.name;
			}
		} else {
			channel = channel.replace('#', '');
			room = await Rooms.findOneByName(channel);
		}

		if (!userId) {
			return;
		}

		const user = await Users.findOneById(userId, { projection: { username: 1, name: 1 } });
		if (!user || !isRegisterUser(user)) {
			throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'archiveRoom' });
		}

		if (!room) {
			void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
				msg: i18n.t('Channel_doesnt_exist', {
					channelName: channel,
					lng: settings.get('Language') || 'en',
				}),
			});
			return;
		}

		if (!(await roomCoordinator.getRoomDirectives(room.t).allowMemberAction(room, RoomMemberActions.ARCHIVE, userId))) {
			throw new Meteor.Error('error-room-type-not-archivable', `Room type: ${room.t} can not be archived`);
		}

		if (!(await hasPermissionAsync(userId, 'archive-room', room._id))) {
			throw new Meteor.Error('error-not-authorized', 'Not authorized');

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Verify the user record exists and has both username and name before dispatching commands
  2. Backfill missing name or username fields on imported users
  3. Terminate sessions of deleted users so stale commands cannot fire
  4. Re-run the command from a healthy account after fixing the user document

Example fix

// before
const user = await Users.findOneById(userId, { projection: { username: 1, name: 1 } });
if (!user || !isRegisterUser(user)) throw new Meteor.Error('error-invalid-user', 'Invalid user');

// after - guard at the caller so the command gets a complete user
if (!user || user.username === undefined || user.name === undefined) {
	return notifyUser('Your profile is incomplete (name/username missing); fix it before using /archive');
}
Defensive patterns

Strategy: validation

Validate before calling

const user = await Users.findOneById(userId, { projection: { username: 1, name: 1 } });
if (!user || user.username === undefined || user.name === undefined) {
	return refuse('Your user record is incomplete (missing name or username)');
}
runCommand('/archive', room);

Type guard

const isRegisterUser = (u: { username?: string; name?: string } | null | undefined): u is { username: string; name: string } =>
	Boolean(u && u.username !== undefined && u.name !== undefined);

Try / catch

catch (err) {
	if (err instanceof Meteor.Error && err.error === 'error-invalid-user') {
		// session references a missing or incomplete user: log out and re-authenticate
	} else throw err;
}

Prevention

When it happens

Trigger: Running /archive from a session whose user was deleted between connection and command processing; users created by imports or bots without a name field; token or session reuse after the account was removed.

Common situations: Deleted-but-still-connected sessions; bulk-imported users missing profile names; broken user-creation extensions that omit required fields.

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18). Data as JSON: /api/errors/cb4268aba49806a4. Report an issue: GitHub.