Why isn't "Username" brought back in the token?

Hi @mike.griffin,

There are probably better ways to code this, but here’s an example rule that adds user.username to the idToken:

function (user, context, callback) {
  user.username = user.username || "";
  context.idToken['https://sso.yourdomain.com/username'] = user.username; 
  callback(null, user, context);
}

The namespace https://sso.yourdomain.com/ can be whatever you want (but not an Auth0 domain). Just needs to be a unique string.

Or if, say, you manage your own username in app_metadata:

function (user, context, callback) {
  user.app_metadata = user.app_metadata || {}; // Not sure if this is required?
  user.app_metadata.username = user.app_metadata.username || "";
  context.idToken['https://sso.yourdomain.com/username'] = user.app_metadata.username; 
  callback(null, user, context);
}

There are useful rule templates available in the Management Console and in this Github repo.

1 Like