RocketChat/Rocket.Chat · error · Meteor.Error
error-unauthorized
error-unauthorized
Error message
Users must have a username
What it means
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.
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, ''] } })
Example fix
// before (server route assumes every user has a username)
API.v1.get('my.route', { authRequired: true }, async function action() { ... });
// after: opt in explicitly when the route tolerates username-less users
API.v1.get('my.route', { authRequired: true, userWithoutUsername: true }, async function action() { ... }); Defensive patterns
Strategy: validation
Validate before calling
const me = await api.get('/api/v1/me');
if (!me?.username) {
throw new Error('Current account has no username - finish registration before using this API');
}
await api.get('/api/v1/some-hono-route'); Type guard
const isUserWithUsername = (user: unknown): user is { username: string } =>
typeof user === 'object' && user !== null && typeof (user as { username?: unknown }).username === 'string' && (user as { username: string }).username.length > 0; Try / catch
try {
await api.get('/api/v1/some-hono-route');
} catch (err) {
if (err.response?.body?.error === 'error-unauthorized' && /username/i.test(err.response?.body?.message ?? '')) {
redirectToUsernameSetup(); // account is half-provisioned; complete registration, then re-issue the token
return;
}
throw err;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- error-action-not-allowed
- error-challenge-expired
- error-challenge-not-found
- error-invalid-challenge-method
- error-invalid-query
AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18).
Data as JSON: /api/errors/64c78c8bb62fabe0.
Report an issue: GitHub.
Appendix: source
Thrown at apps/meteor/server/api/v1/middlewares/authenticationHono.ts:42
const user = await api.authenticatedRoute(convertHonoContextToApiActionContext(c, { logger: options.logger }));
const shouldPreventAnonymousRead = !user && options.authOrAnonRequired && !settings.get('Accounts_AllowAnonymousRead');
const shouldPreventUserRead = !user && options.authRequired;
if (shouldPreventAnonymousRead || shouldPreventUserRead) {
const result = api.unauthorized('You must be logged in to do this.');
// TODO: MAJOR
if (!applyBreakingChanges) {
Object.assign(result.body, {
status: 'error',
message: 'You must be logged in to do this.',
});
}
return c.json(result.body, result.statusCode);
}
if (user && !options.userWithoutUsername && !isUserWithUsername(user)) {
throw new Meteor.Error('error-unauthorized', 'Users must have a username');
}
c.set('user', user);
return next();
};
}
View on GitHub (pinned to b2c16d5842)