Auth0 Home Blog Docs

Hook code not working



I’ve written a pre-registration hook that does not seem to be triggering.

The code works fine when using the test runner. It also errors when creating a user via the Auth0 admin console (as expected as it is referencing firstname and lastname attributes that are not passed through the pipeline), but when registering via lock (which captures the additional attributes) it does not seem to trigger, there are no errors or call to the rest endpoint that the code makes.

Any help would be gratefully appreciated!

The code is:

module.exports = function (user, context, cb) {
request = require("request@2.56.0");

var options = {
 method: 'POST',
 url: '',
   headers: { 
    'Content-Type': 'application/json'
 body: {
      "Email": user.username,
      "FirstName": user.user_metadata.first_name,
      "LastName": user.user_metadata.last_name
 json: true

request(options, function (error, callresponse, body) {
 if (error) cb(new Error(error));
 var response = {};
    response.user = {
    user_metadata: {
            first_name: user.user_metadata.first_name,
            last_name: user.user_metadata.last_name,
            crmcontact: body.CRMContactUUID 
    cb(null, response);

Hi David. Not sure what the error that you are getting is, but you should be able to access a user’s user_metadata from a pre-registration hook. You can try this simpler version (to simplify the code and remove the possibility of the external request failing):

module.exports = function (user, context, cb) {
  // this will output the full user object, and you should
  // be able to see its `user_metadata` object populated
  var response = {};

  response.user = user;

  cb(null, response);

You can watch the console.log(user) output by running the Real-time Webtask Logs.

If you are still not seeing the data, you might want to try a cURL post directly to take Lock also out of the picture, with something like this:

curl -X POST \
  https://YOUR_AUTH0_DOMAIN/dbconnections/signup \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
	"email" : "",
	"user_metadata":{"name": "john","color": "red"}

One final note, that might be obvious but could be easy to miss: remember to select the hook that you’ll want to use as the “active” one in the dropdown list (you can create multiple hooks but choose only one to run).