{"record":{"id":"64c78c8bb62fabe0","repo":"RocketChat/Rocket.Chat","slug":"error-unauthorized","errorCode":"error-unauthorized","errorMessage":"Users must have a username","messagePattern":"Users must have a username","errorType":"exception","errorClass":"Meteor.Error","httpStatus":401,"severity":"error","filePath":"apps/meteor/server/api/v1/middlewares/authenticationHono.ts","lineNumber":42,"sourceCode":"\t\tconst user = await api.authenticatedRoute(convertHonoContextToApiActionContext(c, { logger: options.logger }));\n\t\tconst shouldPreventAnonymousRead = !user && options.authOrAnonRequired && !settings.get('Accounts_AllowAnonymousRead');\n\t\tconst shouldPreventUserRead = !user && options.authRequired;\n\n\t\tif (shouldPreventAnonymousRead || shouldPreventUserRead) {\n\t\t\tconst result = api.unauthorized('You must be logged in to do this.');\n\t\t\t// TODO: MAJOR\n\t\t\tif (!applyBreakingChanges) {\n\t\t\t\tObject.assign(result.body, {\n\t\t\t\t\tstatus: 'error',\n\t\t\t\t\tmessage: 'You must be logged in to do this.',\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn c.json(result.body, result.statusCode);\n\t\t}\n\n\t\tif (user && !options.userWithoutUsername && !isUserWithUsername(user)) {\n\t\t\tthrow new Meteor.Error('error-unauthorized', 'Users must have a username');\n\t\t}\n\n\t\tc.set('user', user);\n\t\treturn next();\n\t};\n}\n","sourceCodeStart":24,"sourceCodeEnd":49,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/api/v1/middlewares/authenticationHono.ts#L24-L49","documentation":"The Hono-based auth middleware (apps/meteor/server/api/v1/middlewares/authenticationHono.ts:42) attaches the authenticated user, then - unless the route opted in via options.userWithoutUsername - rejects requests whose user has no username with Meteor.Error('error-unauthorized', 'Users must have a username'). Rocket.Chat normally assigns usernames at account creation, so a username-less user means a partially provisioned account (created programmatically or interrupted mid-registration) hitting a route that assumes a username exists.","triggerScenarios":"An authenticated REST call on a Hono-mounted route whose options do not set userWithoutUsername: true, made with a token for a user whose username field is missing/empty.","commonSituations":"Accounts created by scripts, apps, or SCIM-style provisioning that skipped username assignment; users stuck between creation and username selection; data migrations that dropped usernames; login-adjacent routes that intentionally allow username-less users clashing with routes that do not.","solutions":["Fix the account: have the user complete registration and pick a username, or set one as admin, then retry","If you own the route definition and it does not need a username, add userWithoutUsername: true to its endpoint options (as the login routes do)","Audit for other affected accounts: db.users.find({ username: { $in: [null, ''] } })"],"exampleFix":"// before (server route assumes every user has a username)\n API.v1.get('my.route', { authRequired: true }, async function action() { ... });\n\n // after: opt in explicitly when the route tolerates username-less users\n API.v1.get('my.route', { authRequired: true, userWithoutUsername: true }, async function action() { ... });","handlingStrategy":"validation","validationCode":"const me = await api.get('/api/v1/me');\nif (!me?.username) {\n  throw new Error('Current account has no username - finish registration before using this API');\n}\nawait api.get('/api/v1/some-hono-route');","typeGuard":"const isUserWithUsername = (user: unknown): user is { username: string } =>\n  typeof user === 'object' && user !== null && typeof (user as { username?: unknown }).username === 'string' && (user as { username: string }).username.length > 0;","tryCatchPattern":"try {\n  await api.get('/api/v1/some-hono-route');\n} catch (err) {\n  if (err.response?.body?.error === 'error-unauthorized' && /username/i.test(err.response?.body?.message ?? '')) {\n    redirectToUsernameSetup(); // account is half-provisioned; complete registration, then re-issue the token\n    return;\n  }\n  throw err;\n}","preventionTips":["Provisioning scripts must always assign a username - never leave accounts half-created","When adding Hono routes, decide explicitly whether username-less users may call them (userWithoutUsername)","After user-creation flows, assert the username exists before handing out tokens"],"tags":["authentication","users","hono","rest-api"],"backgroundTag":"user-missing-username","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}