RocketChat/Rocket.Chat · error · Meteor.Error
error-invalid-user
error-invalid-user
Error message
Invalid user
What it means
The Meteor method wrapper for unreadMessages resolves the caller with Meteor.userId(); when the connection has no authenticated user it throws Meteor.Error('error-invalid-user', 'Invalid user'). The same authentication gate as other user-scoped methods: the invocation arrived without a valid session.
Source
Thrown at apps/meteor/server/lib/messaging/unread/unreadMessages.ts:88
return logger.debug('Provided message is already marked as unread');
}
logger.debug({
msg: 'Updating unread message as the first unread',
timestamp: originalMessage.ts,
});
const setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);
if (setAsUnreadResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(originalMessage.rid, userId);
}
};
Meteor.methods<ServerMethods>({
async unreadMessages(firstUnreadMessage, room) {
methodDeprecationLogger.method('unreadMessages', '9.0.0', '/v1/subscriptions.unread');
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'unreadMessages',
});
}
return unreadMessages(userId, firstUnreadMessage, room);
},
});
View on GitHub (pinned to b2c16d5842)
Solutions
- Check Meteor.userId() before calling; if null, re-authenticate first
- Queue the unread action and re-run it once login resumes
- Authenticate automated clients and keep the token fresh
Example fix
// before
Meteor.call('unreadMessages', { _id }); // logged out
// after
if (Meteor.userId()) {
Meteor.call('unreadMessages', { _id });
} else {
queueAfterLogin(() => Meteor.call('unreadMessages', { _id })); Defensive patterns
Strategy: validation
Validate before calling
if (!Meteor.userId()) {
return redirectToLogin();
}
Meteor.call('unreadMessages', { _id: anchorId }); Type guard
const hasSession = (): boolean => typeof Meteor.userId() === 'string';
Try / catch
Meteor.call('unreadMessages', first, room, (err) => {
if (err?.error === 'error-invalid-user') {
redirectToLogin(); // no authenticated user on this connection
}
}); Prevention
- Guard user-scoped calls with Meteor.userId()
- Queue post-login follow-ups instead of calling while unauthenticated
- Watch for logout-elsewhere and session-expiry stream events
When it happens
Trigger: Meteor.call('unreadMessages', first, room) from a connection that is logged out, still pre-login, or whose token expired; server-side invocation on a connection without a bound user.
Common situations: Session expiry mid-use; scripts firing methods before authentication; tab resumed after the user logged out elsewhere.
Understand the failure class
Background: error-invalid-user: "Invalid user" errors in Rocket.Chat — what they mean and how to fix them — this error's family across 2 libraries.
Related errors
- error-invalid-user
- error-no-message-for-unread
- error-action-not-allowed
- error-subscription-not-found
- error-invalid-user
AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18).
Data as JSON: /api/errors/ef0594edb7f46dcd.
Report an issue: GitHub.