RocketChat/Rocket.Chat · error · Meteor.Error

field_not_found

field_not_found

Error message

Username field "${this.usernameField}" not found in data

What it means

getUsername() resolves the configured usernameField against the identity payload via fromTemplate(); when the result is falsy it throws Meteor.Error('field_not_found', 'Username field "<field>" not found in data') with the payload attached as error details. Note that getUsername's own catch immediately re-wraps it (see the 'Failed to extract username' error), so this inner error is what appears in logs/debug output while clients see the wrapper.

Source

Thrown at apps/meteor/server/lib/auth-providers/custom-oauth/custom_oauth_server.js:319

			const response = await request.json();

			return response.find((email) => email.primary === true)?.email;
		} catch (err) {
			const error = new Error(`Failed to fetch emails from ${this.name} at ${this.emailPath}. ${err.message}`);
			throw _.extend(error, { response: err.response });
		}
	}

	retrieveCredential(credentialToken, credentialSecret) {
		return OAuth.retrieveCredential(credentialToken, credentialSecret);
	}

	getUsername(data) {
		try {
			const value = fromTemplate(this.usernameField, data);

			if (!value) {
				throw new Meteor.Error('field_not_found', `Username field "${this.usernameField}" not found in data`, data);
			}
			return value;
		} catch (error) {
			throw new Error('CustomOAuth: Failed to extract username', error.message);
		}
	}

	getEmail(data) {
		try {
			const value = fromTemplate(this.emailField, data);

			if (!value) {
				throw new Meteor.Error('field_not_found', `Email field "${this.emailField}" not found in data`, data);
			}
			return value;
		} catch (error) {
			throw new Error('CustomOAuth: Failed to extract email', error.message);
		}

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Enable CustomOAuth debug logging and read the 'Username field not found in data' record - it prints the exact payload keys
  2. Correct usernameField to a dot path that exists, e.g. 'preferred_username' or 'user.login'
  3. If using a regex formula, verify it matches the claim value and has exactly one capture group
  4. Remove Username Field entirely to stop strict username extraction when the default behaviour is acceptable

Example fix

// before (Admin -> OAuth -> <service> -> Username Field): 'login'
// payload has no 'login' key -> field_not_found: Username field "login" not found in data

// after
preferred_username   // key that actually exists in the identity payload
Defensive patterns

Strategy: validation

Validate before calling

// before enabling strict mapping, verify the claim exists on a real payload
const sampleIdentity = await fetchIdentityWithTestToken();
const usernameField = 'user.login';
const resolved = usernameField.split('.').reduce<any>((o, k) => (o ? o[k] : undefined), sampleIdentity);

if (!resolved) throw new Error(`usernameField '${usernameField}' does not resolve - do not enable it`);

Type guard

const hasClaim = (payload: Record<string, unknown>, path: string): boolean =>
  path.split('.').reduce<unknown>((o, k) => (o && typeof o === 'object' ? (o as Record<string, unknown>)[k] : undefined), payload) != null;

Try / catch

try {
  identity.username = strategy.getUsername(identity);
} catch (error) {
  logger.warn(error.message);
  identity.username = identity.preferred_username ?? identity.sub; // unique fallback instead of failing login
}

Prevention

When it happens

Trigger: usernameField 'login' but the /me response only contains 'preferred_username'; nested path wrong ('user.login' vs flat 'login'); a '{{/regex/::path}}' formula whose regex does not match so getRegexpMatch returns undefined; provider changed its userinfo schema after an upgrade; usernameField left as empty string (configured constructor coerces missing values to '').

Common situations: Mapping fields by guessing claim names instead of inspecting the payload; provider API version bump renaming claims; Keycloak/Auth0 tenants with different claim sets; dot-path pointing at a key that renameInvalidProperties later mangles (dots become underscores).

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18). Data as JSON: /api/errors/1d44b180aaea2f79. Report an issue: GitHub.