Auth0 Home Blog Docs

Trying to use Hook to add app_metadata but not working


I see that I can add user or app_metadata through Hooks so I created a “Pre User Registration” hook.

The description for pre user registration hooks is “This hook allows custom code to prevent creation of a user in the database or to add custom app_metadata or user_metadata to a newly created user.”

In my case, I’m not trying to prevent a registration but I do want to add custom app_metadata so I figured this is the correct one.

When I created a new hook, it automatically gave me a nice template so I simply removed comment lines. This is what the hook code looks like:

module.exports = function (user, context, cb) {
  var response = {};

  // Add user or app metadata to the newly created user
  response.user = {
   app_metadata: { myapp_user_id: 'some_new_id' }

  response.user = user;

  cb(null, response);

The idea is to generate a UUID that will be the user’s ID in my app. To keep it simple, I decided to start with a simple text – as opposed to a UUID value.

I testing creating a user through the Lock implementation in my app. The user gets created but I don’t see anything in app_metadata so the hook is not working.

Any idea why it’s not working?