{"record":{"id":"1bbf38602a0db65c","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-1bbf38","errorCode":"error-invalid-user","errorMessage":"error-invalid-user","messagePattern":"error-invalid-user","errorType":"exception","errorClass":"Error","httpStatus":401,"severity":"error","filePath":"apps/meteor/server/api/v1/ldap.ts","lineNumber":34,"sourceCode":"\t},\n\trequired: ['message', 'success'] as const,\n\tadditionalProperties: false,\n};\n\nAPI.v1.post(\n\t'ldap.testConnection',\n\t{\n\t\tauthRequired: true,\n\t\tpermissionsRequired: ['test-admin-options'],\n\t\tresponse: {\n\t\t\t200: ajv.compile<{ message: string; success: true }>(messageResponseSchema),\n\t\t\t401: validateUnauthorizedErrorResponse,\n\t\t\t403: validateForbiddenErrorResponse,\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 (settings.get<boolean>('LDAP_Enable') !== true) {\n\t\t\tthrow new Error('LDAP_disabled');\n\t\t}\n\n\t\ttry {\n\t\t\tawait LDAP.testConnection();\n\t\t} catch (err) {\n\t\t\tSystemLogger.error({ err });\n\t\t\tthrow new Error('Connection_failed');\n\t\t}\n\n\t\treturn API.v1.success({\n\t\t\tmessage: 'LDAP_Connection_successful' as const,\n\t\t});\n\t},\n);","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/server/api/v1/ldap.ts#L16-L52","documentation":"Thrown by the POST /api/v1/ldap.testConnection endpoint when this.userId is falsy. Although the route declares authRequired: true and permissionsRequired: ['test-admin-options'], this manual check is a defensive guard inside the action handler. Reaching it means the authentication layer resolved no user ID for the request — the caller is not recognized as a logged-in user.","triggerScenarios":"Calling POST /api/v1/ldap.testConnection without X-Auth-Token / X-User-Id headers, with an expired or revoked token, or with a token whose user account was deleted.","commonSituations":"API session expired between login and the LDAP test call; script or CI job using a stale token from a previous run; personal access token revoked by an admin; calling from a different server instance than where the token was issued.","solutions":["Re-authenticate via POST /api/v1/login (or use a valid personal access token) and pass both X-Auth-Token and X-User-Id headers.","Verify the token is still valid by calling GET /api/v1/me — if it returns 401, generate a new token.","Ensure the user account still exists and has the 'test-admin-options' permission."],"exampleFix":"// before\ncurl -X POST https://rocketchat.example/api/v1/ldap.testConnection\n// after\ncurl -X POST https://rocketchat.example/api/v1/ldap.testConnection \\\n  -H \"X-Auth-Token: ${AUTH_TOKEN}\" \\\n  -H \"X-User-Id: ${USER_ID}\"","handlingStrategy":"validation","validationCode":"// Before calling ldap.testConnection, verify the session is valid\nasync function ensureAuthenticated(api, token, userId) {\n  const res = await fetch(`${api}/api/v1/me`, {\n    headers: { 'X-Auth-Token': token, 'X-User-Id': userId }\n  });\n  return res.ok;\n}\n\nif (!(await ensureAuthenticated(baseUrl, authToken, userId))) {\n  throw new Error('Session expired — re-authenticate before calling ldap.testConnection');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await callLdapTestConnection();\n} catch (e) {\n  if (e.reason === 'error-invalid-user' || e.error === 'error-invalid-user') {\n    // re-authenticate and retry once\n    await login();\n    return callLdapTestConnection();\n  }\n  throw e;\n}","preventionTips":["Always include X-Auth-Token and X-User-Id headers in API calls to authenticated endpoints.","Implement token refresh logic that detects 401 responses and re-authenticates.","Store tokens securely and verify validity with GET /api/v1/me before batch operations."],"tags":["ldap","authentication","api","admin"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}