Migrating from Hooks for Actions does not work. How to fix it?

I have a Pre User Registration hook that sets a custom field is_approved to user. and this is the code:

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

  response.user = user;

  // Add user or app metadata to the newly created user
  // Adding is_approved attribute to app_metadata and setting it to false
  response.user.app_metadata = { is_approved: false };

  // Continue with the response
  cb(null, response);
};

Since hooks are going to be deprecated, I want to create use actions. So I created this action:

exports.onExecutePreUserRegistration = async (event, api) => {
    api.user.setAppMetadata('is_approved', false)
};

This should add is_approved field to app metadata in username/password logins, but it does not add. In addition, I added another Action to add this field in social logins:

exports.onExecutePostLogin = async (event, api) => {
    if (event.stats.logins_count == 1) {
      api.user.setAppMetadata('is_approved', false)
    }
};

I deployed the actions and disabled the hook. I registered a user but the app metadata is empty which means that these actions are not working.

Hey there @mahdi1 welcome to the community!

This code should work - Do you have any hooks or rules enabled that could be interfering? I would double check to see if you have any enabled, and if you do remove them and test again.

Keep us posted!

Hey @tyf. Thanks for answering. I have one hook and two rules. I disabled all of them and registered a user. But still nothing. Could you please help?

1 Like

I’m having a hard time coming up with anything else to check - Is the user you are registering a Auth0 database user or social (google, facebook, etc.), passwordless, etc.?

Thanks for your reply @tyf. I registered the user with Google (social login).

Hey @mahdi1 no problem, happy to help where I can!

Thanks for confirming - Only the Post Login Action will run on Social logins. The Post Login Action code you shared above seems to be working in my environment when logging in for the first time with a Google user:

exports.onExecutePostLogin = async (event, api) => {
    if (event.stats.logins_count == 1) {
      api.user.setAppMetadata('is_approved', false)
    }
};

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