Updating user.name in Auth0 Rule

Hi, I have a simple Auth0 rule where I’d like to set the user.name property to the provided family_name and given_name. By default, the name property is just the user’s provided email; however, I have the family_name and given_name in the sign up form so I’d like to just concat the two together as the name property.

The rule I have looks something like the below code and does in fact update the Auth0 user properly; however, I have to sign out and sign back in before my apps get the correctly updated name value. I assume the issue is that the generated JWT tokens aren’t getting the value when the name is updated within the rule itself although I’m not sure what I should be doing instead to make sure it’s updated in time for my apps to get the new value.

My Auth0 rule looks like the following:

function addAttributes(user, context, callback) {
  const ManagementClient = require('auth0@2.23.0').ManagementClient;

  const management = new ManagementClient({
    token: auth0.accessToken,
    domain: auth0.domain
  });
  
  if((user.name === undefined || user.name === user.email) && user.given_name !== undefined) {
    var name = user.given_name;
    if(user.family_name !== undefined) {
      name += " " + user.family_name;
    }
    user.name = name;
    management.updateUser({ id: user.user_id }, { name: name }, function(err,user) {
      if (err) {
        // Handle error
        console.error(err);
      }
      callback(null, user, context);
    });
  } else {
    callback(null, user, context);
  }
}

According to the order of events, I should be able to make this kind of modification to the user record in an Auth0 Rule. Upon inspection of the idToken and accessToken within the rule I didn’t see any field related to the user.name that I needed to update beyond what I’ve already done in the above rule (e.g. I assumed that all I had to do was set user.name = name). Any suggestions would be appreciated!

Hey there!

As Rules & Hooks are being fully deprecated soon, maybe you’re interested in finding out more how to achieve that with Actions?

As this topic is related to Rules - Hooks - Actions, I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!