RocketChat/Rocket.Chat · error · Meteor.Error
error-invalid-user
error-invalid-user
Error message
Invalid user
What it means
Thrown inside saveNotificationSettingsMethod's helper getNotificationPrefValue when the requested value is 'default' and the userId param is empty. Resolving 'default' requires reading the caller's server-level user preference, which is impossible without a user; the code guards that with 'error-invalid-user'. The exported method receives userId from its callers, so this fires when it is invoked with a falsy user id (unauthenticated DDP call or a server-side caller passing '').
Source
Thrown at apps/meteor/server/meteor-methods/users/saveNotificationSettings.ts:41
| 'audioNotificationValue';
declare module '@rocket.chat/ddp-client' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
saveNotificationSettings(roomId: string, field: NotificationFieldType, value: string): boolean;
saveAudioNotificationValue(subId: string, value: string): boolean;
}
}
export const saveNotificationSettingsMethod = async (
userId: string,
roomId: string,
field: NotificationFieldType,
value: string,
): Promise<boolean> => {
const getNotificationPrefValue = async (field: string, value: unknown) => {
if (value === 'default') {
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'saveNotificationSettings',
});
}
const userPref = await getUserNotificationPreference(userId, field);
return userPref?.origin === 'server' ? null : userPref;
}
return { value, origin: 'subscription' };
};
const notifications = {
desktopNotifications: {
updateMethod: async (subscription: ISubscription, value: unknown) =>
Subscriptions.updateNotificationsPrefById(
subscription._id,
await getNotificationPrefValue('desktop', value),
'desktopNotifications',
'desktopPrefOrigin',View on GitHub (pinned to b2c16d5842)
Solutions
- Authenticate before calling: ensure Meteor.userId() is set (or pass a real user id to the exported method).
- In server code, skip the call when no user context exists instead of passing '' as userId.
- Use the REST endpoint POST /v1/rooms.saveNotification with valid auth credentials.
Example fix
// before: fires after logout because value is 'default' and userId is falsy
await saveNotificationSettingsMethod('', rid, 'desktopNotifications', 'default');
// after: only resolve 'default' with a real user; otherwise store the literal value
if (value === 'default' && !userId) throw new Meteor.Error('error-invalid-user', 'Invalid user');
await saveNotificationSettingsMethod(userId, rid, 'desktopNotifications', value); Defensive patterns
Strategy: type-guard
Validate before calling
const uid = Meteor.userId();
const safeValue = uid ? value : (value === 'default' ? 'all' : value);
// or skip the call entirely when resolving 'default' is impossible:
if (value === 'default' && !uid) return showError('Log in to use default preferences');
await Meteor.callAsync('saveNotificationSettings', rid, field, value); Type guard
const hasUserContext = (userId: string | null | undefined): userId is string => typeof userId === 'string' && userId.length > 0;
Try / catch
try {
await saveNotificationSettingsMethod(userId, rid, field, value);
} catch (e) {
if ((e as Meteor.Error).error === 'error-invalid-user' && value === 'default') {
// resolving 'default' needs a user; fall back to an explicit value
await saveNotificationSettingsMethod(userId, rid, field, 'all');
}
} Prevention
- Never invoke saveNotificationSettingsMethod with an empty userId and value 'default'
- Resolve 'default' to a concrete value when no user context exists
- Keep the method wrapper's Meteor.userId() guard as the single source of the user id
When it happens
Trigger: Meteor.call('saveNotificationSettings', roomId, 'desktopNotifications', 'default') while logged out (wrapper passes Meteor.userId() = null downstream in older paths); server code importing saveNotificationSettingsMethod and passing an empty userId with value 'default'.
Common situations: Preference panels that submit 'default' after session expiry; background jobs reusing the method without a user context.
Related errors
AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18).
Data as JSON: /api/errors/2ed00fceb8209cc8.
Report an issue: GitHub.