{"record":{"id":"6fd002a6700b9af2","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user","errorCode":"error-invalid-user","errorMessage":"error-invalid-user","messagePattern":"error-invalid-user","errorType":"http","errorClass":"Error","httpStatus":401,"severity":"error","filePath":"apps/meteor/ee/server/api/ldap.ts","lineNumber":32,"sourceCode":"\trequired: ['message', 'success'],\n\tadditionalProperties: false,\n});\n\nAPI.v1.post(\n\t'ldap.syncNow',\n\t{\n\t\tauthRequired: true,\n\t\tforceTwoFactorAuthenticationForNonEnterprise: true,\n\t\ttwoFactorRequired: true,\n\t\tresponse: {\n\t\t\t200: ldapSyncNowResponseSchema,\n\t\t\t400: validateBadRequestErrorResponse,\n\t\t\t401: validateUnauthorizedErrorResponse,\n\t\t},\n\t},\n\tasync function action() {\n\t\tif (!this.userId) {\n\t\t\tthrow new Error('error-invalid-user');\n\t\t}\n\n\t\tif (!(await hasPermissionAsync(this.user, 'sync-auth-services-users'))) {\n\t\t\tthrow new Error('error-not-authorized');\n\t\t}\n\n\t\tif (settings.get('LDAP_Enable') !== true) {\n\t\t\tthrow new Error('LDAP_disabled');\n\t\t}\n\n\t\tawait LDAPEnterprise.sync();\n\t\tawait LDAPEnterprise.syncAvatarAndAbacAttributes();\n\n\t\treturn API.v1.success({\n\t\t\tmessage: 'Sync_in_progress' as const,\n\t\t});\n\t},\n);","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/ee/server/api/ldap.ts#L14-L50","documentation":"Thrown by the POST ldap.syncNow endpoint when this.userId is falsy. Because the route declares authRequired:true, the framework middleware should have rejected the request with 401 before the handler runs; reaching this throw indicates the auth context was lost or the route middleware was bypassed. It is effectively a defensive guard against a missing user session. Code is the raw string 'error-invalid-user' (plain Error, not Meteor.Error).","triggerScenarios":"Calling POST /v1/ldap.syncNow with a missing/invalid auth token that somehow passed the authRequired gate, or invoking the action function outside the normal API middleware pipeline (e.g. direct unit test without seeding this.userId).","commonSituations":"Token expired between auth middleware and handler in a long-lived request; test harness calling the handler directly without mocking this.userId; custom middleware that strips the user object.","solutions":["Authenticate the request: send a valid X-Auth-Token + X-User-Id header (or resume token) so the framework populates this.userId.","If testing, seed this.userId on the request context before invoking the action.","Audit any custom middleware on the ldap.syncNow route that could clear this.user.","Confirm the API framework version correctly runs authRequired before the action."],"exampleFix":"// before (test): handler invoked with no user context\nawait action.call({});\n\n// after: seed userId on the bound context\nawait action.call({ userId: 'rocketchat.internal.admin.test', user: adminUser });","handlingStrategy":"validation","validationCode":"// Ensure auth context is present before invoking the handler (e.g. in tests)\nfunction assertUserId(ctx: { userId?: string }): asserts ctx is { userId: string } {\n  if (!ctx.userId) throw new Error('error-invalid-user');\n}\nassertUserId(this);","typeGuard":"function hasUserId(ctx: unknown): ctx is { userId: string; user: unknown } {\n  return typeof ctx === 'object' && ctx !== null && typeof (ctx as any).userId === 'string';\n}","tryCatchPattern":"try {\n  await api.post('ldap.syncNow', {});\n} catch (e) {\n  if (e.message === 'error-invalid-user') {\n    // re-authenticate, then retry once\n    await relogin();\n    return api.post('ldap.syncNow', {});\n  }\n  throw e;\n}","preventionTips":["Always send valid X-Auth-Token + X-User-Id headers on protected endpoints.","In tests, seed this.userId on the bound action context."],"tags":["auth","ldap","rest-api","defensive-guard"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}