{"record":{"id":"fc08c7a34f92682d","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-fc08c7","errorCode":"error-invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/rooms/toggleFavorite.ts","lineNumber":40,"sourceCode":"\n\tconst { modifiedCount } = await Subscriptions.setFavoriteByRoomIdAndUserId(rid, userId, favorite);\n\n\tif (modifiedCount) {\n\t\tvoid notifyOnSubscriptionChangedByRoomIdAndUserId(rid, userId);\n\t}\n\n\treturn modifiedCount;\n};\n\nMeteor.methods<ServerMethods>({\n\tasync toggleFavorite(rid, favorite) {\n\t\tmethodDeprecationLogger.method('toggleFavorite', '9.0.0', '/v1/rooms.favorite');\n\t\tcheck(rid, String);\n\t\tcheck(favorite, Match.Optional(Boolean));\n\t\tconst userId = Meteor.userId();\n\n\t\tif (!userId) {\n\t\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\t\tmethod: 'toggleFavorite',\n\t\t\t});\n\t\t}\n\n\t\treturn toggleFavoriteMethod(userId, rid, favorite);\n\t},\n});\n","sourceCodeStart":22,"sourceCodeEnd":48,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/rooms/toggleFavorite.ts#L22-L48","documentation":"The Meteor method wrapper for toggleFavorite throws error-invalid-user when Meteor.userId() is null — there is no authenticated user on the connection. The method is also deprecated since 9.0.0 in favor of the REST endpoint POST /api/v1/rooms.favorite.","triggerScenarios":"Calling Meteor.call('toggleFavorite', ...) before the login handshake completes, with an expired or revoked token, or from server code with no bound user on the connection.","commonSituations":"Auth races at page load; sessions dropped after password change; integrations still on the legacy method instead of the REST rooms.favorite endpoint after 9.0.0.","solutions":["Ensure login completed (Meteor.userId() non-null) before calling; re-authenticate on token expiry","Migrate to REST: POST /api/v1/rooms.favorite with { roomId, favorite } and an auth token","Gate favorite actions on an authenticated session state so logged-out clients never fire the method"],"exampleFix":"// before\nMeteor.call('toggleFavorite', rid, true);\n\n// after (9.0.0+)\nawait fetch('/api/v1/rooms.favorite', {\n  method: 'POST',\n  headers: { 'X-Auth-Token': token, 'X-User-Id': uid, 'Content-Type': 'application/json' },\n  body: JSON.stringify({ roomId: rid, favorite: true }),\n});","handlingStrategy":"validation","validationCode":"const userId = Meteor.userId();\nif (!userId) {\n  // complete login first; or use REST POST /api/v1/rooms.favorite\n}\nawait Meteor.callAsync('toggleFavorite', rid, true);","typeGuard":null,"tryCatchPattern":"try {\n  await Meteor.callAsync('toggleFavorite', rid, favorite);\n} catch (error) {\n  if (error instanceof Meteor.Error && error.error === 'error-invalid-user') {\n    // unauthenticated: re-login, then retry or switch to the REST endpoint\n  }\n}","preventionTips":["Guard favorite calls on a non-null Meteor.userId()","Migrate off the deprecated method: POST /api/v1/rooms.favorite with { roomId, favorite }","Handle session expiry centrally instead of per-call"],"tags":["authentication","favorites","meteor-methods","deprecation"],"backgroundTag":"user-not-authenticated","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"}