RocketChat/Rocket.Chat · error · Meteor.Error

error-invalid-user

error-invalid-user

Error message

Invalid user

What it means

Inside BotHelpers.addUserToRoom, Meteor.userId() returned null - the helper is executing without an authenticated user context. The botRequest DDP method authenticates the bot user before dispatching, so in practice this fires when the helper path is invoked from server code outside a user-bound method context, or when the session's user is lost across an await.

Source

Thrown at apps/meteor/server/lib/bot-helpers/index.ts:83

	async addUserToRole(userName: string, roleId: string, userId: string): Promise<void> {
		await addUserToRole(userId, roleId, userName);
	}

	async removeUserFromRole(userName: string, roleId: string, userId: string): Promise<void> {
		await removeUserFromRole(userId, roleId, userName);
	}

	async addUserToRoom(userName: string, room: string): Promise<void> {
		const foundRoom = await Rooms.findOneByIdOrName(room);

		if (!foundRoom) {
			throw new Meteor.Error('invalid-channel');
		}

		const userId = Meteor.userId();
		if (!userId) {
			throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'addUserToRoom' });
		}
		await addUsersToRoomMethod(userId, {
			rid: foundRoom._id,
			users: [userName],
		});
	}

	async removeUserFromRoom(userName: string, room: string) {
		const foundRoom = await Rooms.findOneByIdOrName(room);

		if (!foundRoom) {
			throw new Meteor.Error('invalid-channel');
		}
		const userId = Meteor.userId();
		if (!userId) {
			throw new Meteor.Error('error-invalid-user', 'Invalid user');
		}
		await removeUserFromRoomMethod(userId, { rid: foundRoom._id, username: userName });

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Invoke the helper through the authenticated 'botRequest' DDP method so a user context exists
  2. Thread the userId explicitly through your server code instead of relying on Meteor.userId()
  3. Re-establish the bot's session (re-login) before retrying
Defensive patterns

Strategy: validation

Validate before calling

// server-side, before invoking the helper path:
const uid = Meteor.userId();
if (!uid) {
  // no authenticated user context - route the call through 'botRequest' instead
}

Try / catch

try {
  await call('botRequest', 'addUserToRoom', username, room);
} catch (e) {
  if (e instanceof Meteor.Error && e.error === 'error-invalid-user') {
    // session lost - re-authenticate the bot user and retry
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling botHelpers.addUserToRoom from a cron job, app startup, or server fiber without a Meteor user; a long-running await resuming after the session was invalidated; test code invoking the helper directly.

Common situations: Refactors moving bot calls out of the authenticated request path; logout racing an in-flight bot request.

Related errors


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