Cannot get additional fields in a rule after Sign Up

Hey Folks!

I’m having an issue where I need to have the name at sign up, I already did the additional field in lock (snippet down), then I need to update the root attribute for name, using that additional field. The problem I’m facing is that I’m not able to get that additional field in the rule, whithin user_metadata nor app_metadata after sign up.

Any help is welcome.

Additional Filed Snippet:
additionalSignUpFields: [{
name: “full_name”,
placeholder: “Enter your full name”,
validator: function (name) {
return {
valid: name.length !== 0,
hint: “Please fill your name”
}
}
}]

Rule snippet:

function addUserNameRootAttrs(user, context, callback) {
  user.user_metadata = user.user_metadata || {};
  console.log('Users\'s object: ', user);
  console.log('Users\'s context object: ', context);
  if (user.user_metadata.profile.name !== user.name) {
    var ManagementClient = require('auth0@2.23.0”').ManagementClient;
    var management = new ManagementClient({
      clientId: configuration.AUTH0_CLIENT_ID,
      clientSecret: configuration.AUTH0_SECRET,
      domain: configuration.AUTH0_DOMAIN,
      scopes: ['update:profile'],
    });

    var params = { id: user.user_id };
    var data = { name: user.user_metadata.profile.name };
    management.users.update(params, data, function (err, user) {
      if (err) {
        throw err;
      }
      console.log('New Profile: ', user);
      callback(null, user, context);
    });
  }

  callback(null, user, context);
}

Thanks!

Hi @ricardo.jimenez,

Welcome to the Community!

The name is being stored as user.user_metadata.full_name instead of user.user_metadata.profile.name.

You can adjust your rule like this:

function addUserNameRootAttrs(user, context, callback) {
  user.user_metadata = user.user_metadata || {};
  console.log('Users\'s object: ', user);
  console.log('Users\'s context object: ', context);
  if (user.user_metadata.full_name !== user.name) {
    var ManagementClient = require('auth0@2.23.0').ManagementClient;
    var management = new ManagementClient({
      domain: auth0.domain,
      token: auth0.accessToken
    });

    var params = { id: user.user_id };
    var data = { name: user.user_metadata.full_name };
    management.users.update(params, data, function (err, user) {
      if (err) {
        throw err;
      }
      console.log('New Profile: ', user);
      callback(null, user, context);
    });
  }
  callback(null, user, context);
}

Alternatively, you can use the field name "name" and pass the storage: "root" parameter to lock and you will no longer need a rule to accomplish this.

      additionalSignUpFields: [{
        name: "name",
        placeholder: "Enter your full name",
        storage: "root",
        validator: function (name) {
          return {
            valid: name.length !== 0,
            hint: "Please fill your name"
          }
        }
      }]

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.