Limit hook to specific applications

I want to be able to limit the code that is run within a pre user registration hook to one specific client. I came across this which seems to be a good solution, but I am having trouble getting it to work.

For the application that I want to use the logic within the hook, I added a key in application metadata named “isPreUserRegistrationHookActive” with a value of true. I created a rule with the following logic:

function (user, context, callback) {  
  context.clientMetadata = context.clientMetadata || {};
  
  if(context.clientMetadata.isPreUserRegistrationHookActive === true){
    context.connectionMetadata = context.connectionMetadata || {};
    context.connectionMetadata.isPreUserRegistrationHookActive = true;
  }
  
  callback(null, user, context);
}

When I test this rule and add that property to context.clientMetadata, I can see that the property is added to connectionMetadata in the rule output. Finally, I created the following conditional statement within my hook:

context.connectionMetadata = context.connectionMetadata || {}; 
if(context.connectionMetadata.isPreUserRegistrationHookActive === true){
// stuff
}

The logic within that if statement is not evaluated when I believe the rule should be setting the connectionMetadata property. If I use the runner and set the property myself, the logic is evaluated. What am I missing?

Is there a better way to do this? I suppose I could have the application set a user_Metadata property to control the behavior, but I would rather not leave it up to the client (caller) to decide whether or not this logic is evaluated.

2 Likes

Is your hook pre-registration or post-registration? If it is former then Pre-registration hook will run before any rules are executed. Rules are executed post successful authentication.

1 Like

Ahhhh, that makes sense, thank you. Yes, my hook is pre-registration.

Is there any other way to access an application’s metadata within a hook? For now I am using the approach I mentioned at the end of my question (user_Metadata).

Hey there!

As this topic is related to Rules - Hooks - Actions and Rules & Hooks are being deprecated soon I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!