{"record":{"id":"ef0594edb7f46dcd","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-ef0594","errorCode":"error-invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/messaging/unread/unreadMessages.ts","lineNumber":88,"sourceCode":"\t\treturn logger.debug('Provided message is already marked as unread');\n\t}\n\n\tlogger.debug({\n\t\tmsg: 'Updating unread message as the first unread',\n\t\ttimestamp: originalMessage.ts,\n\t});\n\tconst setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);\n\tif (setAsUnreadResponse.modifiedCount) {\n\t\tvoid notifyOnSubscriptionChangedByRoomIdAndUserId(originalMessage.rid, userId);\n\t}\n};\n\nMeteor.methods<ServerMethods>({\n\tasync unreadMessages(firstUnreadMessage, room) {\n\t\tmethodDeprecationLogger.method('unreadMessages', '9.0.0', '/v1/subscriptions.unread');\n\t\tconst userId = Meteor.userId();\n\t\tif (!userId) {\n\t\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\t\tmethod: 'unreadMessages',\n\t\t\t});\n\t\t}\n\n\t\treturn unreadMessages(userId, firstUnreadMessage, room);\n\t},\n});\n","sourceCodeStart":70,"sourceCodeEnd":96,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/messaging/unread/unreadMessages.ts#L70-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"Session expiry mid-use; scripts firing methods before authentication; tab resumed after the user logged out elsewhere.","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"],"exampleFix":"// before\nMeteor.call('unreadMessages', { _id }); // logged out\n\n// after\nif (Meteor.userId()) {\n  Meteor.call('unreadMessages', { _id });\n} else {\n  queueAfterLogin(() => Meteor.call('unreadMessages', { _id }));","handlingStrategy":"validation","validationCode":"if (!Meteor.userId()) {\n  return redirectToLogin();\n}\nMeteor.call('unreadMessages', { _id: anchorId });","typeGuard":"const hasSession = (): boolean => typeof Meteor.userId() === 'string';","tryCatchPattern":"Meteor.call('unreadMessages', first, room, (err) => {\n  if (err?.error === 'error-invalid-user') {\n    redirectToLogin(); // no authenticated user on this connection\n  }\n});","preventionTips":["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"],"tags":["unread","authentication","ddp-method"],"backgroundTag":"authentication-required","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}