Auth0 Home Blog Docs

Auth0 hooks are not working

I’m using the following hook ( Pre User Registration ). However it is not working and nothing happens.
I don’t understand this because the code was already found in some of the Auth0 documentation.

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

  // Add app metadata to the newly created user
  response.user = {
   app_metadata: { roles: ["Not Approved"]}
  };

  response.user = user;
  cb(null, response);
};

Help is very much appreciated! Thanks.

1 Like

Hey there @maximilian_cs!

Not sure if it’s a temporary bug as everything looks fine. Will try to look internally. Could you let me know in a few moments if it still doesn’t work?

1 Like

Sure let me dig into it and will get back to you once I have news to share!

1 Like

I’m also having trouble getting hooks to fire on a login event.

Hey there @greg.dennis!

Exactly the same as above?

Hi @konrad.sopala

I am facing the same issue as well.

I have posted a si.ilar question as well.

Hey there everyone!

Thank you for reporting that! One of our teams is looking into it and i will relay their message as soon as I can.

Pretty much the same. Hooks are not being executed. Only difference is I’m use a credentials hook.

Hey there @maximilian_cs!

I was inspecting you code snippet once more and it seems like you are setting user.app_metadata, then subsequently overwriting it with response.user = user. Can I ask you where did you take this code snippet from?

Here’s my suggestion:

var response = {};

user.app_metadata = user.app_metadata || {};
if (user.app_metadata.roles)

{ user.app_metadata.roles.push("Not Approved"); }

else

{ user.app_metadata.roles = ["Not Approved"]; }

response.user = user;

The above hook is firing.

Hi @konrad.sopala

I tried adding the section of code you provided. It still isn’t working.

module.exports = function(client, scope, audience, context, cb) {
  var response = {};

user.app_metadata = user.app_metadata || {};
if (user.app_metadata.roles)

{ user.app_metadata.roles.push("Not Approved"); }

else

{ user.app_metadata.roles = ["Not Approved"]; }

response.user = user;

cb(null, response);

};

Has it got anything to do with API Settings? or any other component like Roles? How does user fit into either of client, scope, audience or context? By looking at the code, I am assuming the code you have mentioned is for Rules and not Hooks.

I have an update guys.

Just experimenting around with the code @konrad.sopala had suggested. It looks like

context.accessToken['https://example.com/role'] = 'admin';

should be called, but also note that it should be called within

auth0.users.updateUserMetadata(user.user_id, user.user_metadata)

This below code snippet is working for me. I am able to see my custom claim in the payload of access token.

Adding this code under Rules and NOT under Hooks.

function (user, context, callback) {
  
  user.user_metadata = user.user_metadata || {};

  auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
    .then(() => {
      context.accessToken['https://example.com/role'] = 'admin';
      callback(null, user, context);
    })
    .catch((err) => {
      callback(err);
    });
  
}

There is still no clear documentation which talks about when Rules and Hooks should be used.

1 Like

The hook is still not firing for me.

module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;

  access_token.scope.push("a.discretely.added.scope");

  cb(null, access_token);
};

I should get a token with the above scope encoded into it, but the token has no scopes.

Note that when I run it in the editor, the return shows that the scope is added.


I tried creating a rule, but that’s not working either. It fires, but I don’t get a token when the rule is active.

function (user, context, callback) {
  
  if (context.accessToken.scope === undefined) {
    context.accessToken.scope = [];
  }
  context.accessToken.scope.push('added.scope.in.rule');
  
  callback(null, user, context);
}

When I run this in the editor, I get a response that the scope is added to the accessToken property.


To test both of these, I’m using this WPF sample.

@ved.stack thank you for reporting that! Can you either provide more context around what specifically Rules and Hooks documentation lacks so I can relay to our product team or if you want you can directly submit it to the product team via our feedback site (you will be contacted by one of our product managers soon):

Also failing to get a pre-user-registration hook to successfully add app_metadata: the preview works just fine, but nothing seems to happen during an actual user login? Looking at the new user’s info via the Users console, no app_metadata exists.

module.exports = function (user, context, cb) {
  const uuid = require('uuid');
  const newId = uuid();
  var response = {};
  user.app_metadata = user.app_metadata || {};
  user.app_metadata.id  = newId;
  response.user = user;

  cb(null, response);
};

Im having the same Issue with user_metada. Not even the example in the documentation is working. The hook preview works fine, but then when i test it with a “new user” the metadata section is empty

I can also not get any examples of hooks to work. It seems like the hook feature is not working.

Hey there!

Can you let me know if you have any error message appearing or what (screenshot would be best). I need more context in order to reproduce, debug or simply inform appropriate team about it. Thanks!