Pre User Registration - modify usename with user_metadata value

Is it possible to create a Pre User Registation Hook to modify username value with a user metadata field?

I have a custom signup field and I need save user with this value as username.

Our docs reference something along these lines - have you had a chance to review this?

Yes, I have reviewed docs…

I have created a Pre-User Registration hook:

/**
@param {object} user - The user being created
@param {string} user.tenant - Auth0 tenant name
@param {string} user.username - user name
@param {string} user.password - user's password
@param {string} user.email - email
@param {boolean} user.emailVerified - is e-mail verified?
@param {string} user.phoneNumber - phone number
@param {boolean} user.phoneNumberVerified - is phone number verified?
@param {object} context - Auth0 connection and other context info
@param {string} context.requestLanguage - language of the client agent
@param {object} context.connection - information about the Auth0 connection
@param {object} context.connection.id - connection id
@param {object} context.connection.name - connection name
@param {object} context.connection.tenant - connection tenant
@param {object} context.webtask - webtask context
@param {function} cb - function (error, response)
*/
module.exports = function (user, context, cb) {
  var response = {};
  response.user = {
    user_metadata: { nombre: 'unnombre', apellidos: 'unosapell' },
    usename: 'customusername'
  };

  cb(null, response);
};

User metadata is replaced, but username not. So, I would like to know if to modify username value with a user metadata field is possible, or it’s not possible to do that.

When I run hook in dashboard I get the response:

{
  "user": {
    "user_metadata": {
      "nombre": "unnombre",
      "apellidos": "unosapell"
    },
    "usename": "customusername"
  }
}

But when I sign up by Login by Auth0 wordpress plugin, username is not being changed…

My first thought is that you have "usename" instead of username and that elsewhere in your code it might be causing issues?

Outside of that, you mention the Wordpress plugin - I’m wondering if @josh.cunningham has seen this before?

I’m not sure if I totally follow what you’re trying to do here @melmenlu but we have a number of hooks in WordPress (not to be confused with the hooks in Auth0) that might be what you’re looking for:

In the Actions section, you’ll see a couple of hooks that can change how you look up and create WordPress users. You’ll have access to the complete Auth0 profile that was returned as well as the User that was created or matched during that process.

I am trying to use hooks in Outh0, not in WordPress.

I want to create a Pre-User Registration hook that set username with a value. Please, could you indicate me if it’s possible to do that?

You mentioned WordPress so I wasn’t sure if the issue was in the plugin (sending the right information but not saving properly) or in the pre-reg hook.

Did you try adjusting the spelling of usename like @jerdog suggested?

Yes, I adjusted the spelling of username.

This is my Pre-User Registration hook created in dashboard:

/**
@param {object} user - The user being created
@param {string} user.tenant - Auth0 tenant name
@param {string} user.username - user name
@param {string} user.password - user's password
@param {string} user.email - email
@param {boolean} user.emailVerified - is e-mail verified?
@param {string} user.phoneNumber - phone number
@param {boolean} user.phoneNumberVerified - is phone number verified?
@param {object} context - Auth0 connection and other context info
@param {string} context.requestLanguage - language of the client agent
@param {object} context.connection - information about the Auth0 connection
@param {object} context.connection.id - connection id
@param {object} context.connection.name - connection name
@param {object} context.connection.tenant - connection tenant
@param {object} context.webtask - webtask context
@param {function} cb - function (error, response)
*/
module.exports = function (user, context, cb) {
  var response = {};
  response.user = {
    "username": "blabla"
  };

  cb(null, response);
};

It’s not working. Username is not being changed. I’m using Login by Auth0 3.7.0.

Is the data being saved to the user in the Auth0 dashboard? If not, then the troubleshooting should continue in this pre-reg hook.

This is my pre user registration hook code:

  var response = {};
  response.user = {
    "username": "usuario00s0"
  };
  cb(null, response);
};

I have deactivate all additionalSignUpFields in Login by Auth0 plugin.

I can see user created in the Auth0 dashboard, but username is not being changed by “usuario00s0”. So, hook is not working… But, why not?

If I user this code in my hook (only for testing):

  var response = {};
  response.user = {
    user_metadata: { 
      dni: user.user_metadata.dni,
      nombre: user.user_metadata.dni, 
      apellidos: user.user_metadata.dni
    },
    "username": "usuario00s0"
  };

  cb(null, response);
};

I can see user created in the Auth0 dashboard, user_metadata modified correctly with same value but username is not being changed by “usuario00s0”.

So, is it possible to create a Pre User Registation Hook to modify username value?
What I am experiencing makes me think that it is not possible…

In testing this out, it doesn’t look like anything can change besides the metadata. That makes sense to me because if someone registers with a specific username and the system changes it, how are they supposed to know how to log in? This would just change it so something else without notification.

Sorry this isn’t clear in the documentation, I’ll work with the team to get that updated.

Another idea might be to reject the registration if a particular field is not formatted properly or has an invalid value. Since you can access the username in that hook, you could check that against whatever validation rules you’d like.

Josh, I was actually just implementing something similar this morning and it works. We have a similar requirement where the username has to be restricted to letters, numbers and underscore so we implement the following in the pre-user registration hook:

 if (!/^[A-Za-z]\w*$/.test(user.username)) {
     cb(new Error("Username must start with a letter and only contain letters, numbers or underscore characters"));
  } else {
    cb(null, {user});
  }

The problem is however that all that is returned to the sign up form is the the message ‘We’re sorry, something went wrong when attempting to sign up.’ rather than the message provided in the error. It’s not particularly useful to just have a generic error returned to the user when we can supply more specific information.

I see from other posts that this is a known issue but is there any timeline on being able to provide custom error messages on pre-user registration errors?

I have same issue. So, I was trying to use one additional signup field with validation, and modifying it in pre user registration hook (but it’s not possible).

It would be very helpful to be able to show the error message in the form. I’m also very interested in knowing the timeline…

@brendan @melmenlu - It’s still in the backlog. I’ll check again on an expected timeframe.

Thanks Josh, to be honest it’s not particularly useful to be able to reject signup without being able to tell the user why they can’t sign up.

I totally agree and the engineering team tasked with this does as well! Our highest priority is security and this particular change takes a bit of planning but it should be released here in the near future. I don’t have a specific date but it is in the works.

Thanks for your patience!

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