Auth0 Home Blog Docs

How to implement the custom webhook rule using Web API 2?

rules
webapi

#1

I’m trying to use the rule for custom webhook using ASP .NET Web API 2. What i want to implement is whenever a user registers for the first time, Auth0 should make a call to my server and I would generate a custom user ID which would be added to the user metadata.

I tried the implementation and I was getting a user does not exist error. Here is my code:

function (user, context, callback) {
  user.app_metadata = user.app_metadata || {};
  if (user.app_metadata.customId) {
    console.log('Found ID!');
    return callback(null, user, context);
  }
  request.post({
    url: 'http://test.taskub.com//api/users',
    json: {
      user: user,
      context: context,
      secretToken: "userid",
    },
    timeout: 15000
  }, function(err, response, body){
    if (err) return callback(new Error(err));

    user.app_metadata.customId = body.customId;
    context.idToken'https://taskub.com/api/user/custom_id'] = body.customId;

    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
      .then(function(){
        callback(null, user, context);
      })
      .catch(function(err){
        callback(err);
      });
  });
}

#2

I created a rule very similar to the one you posted (called a different URL instead of the example one you included) and could not reproduce the issue. The hook was called and the custom ID was added to the user metadata.

Have in mind that if the rule calls Auth0 API’s directly or indirectly (in this case it updates user metadata) using the TRY THIS RULE option with the default mock data will lead to unspecified behavior depending on rule logic. In this particular scenario, trying that rule using that option will indeed lead to a The user does not exist. error because the fake data passed to the rule is not associated with a real user.

You can either ensure that both the user and context data passed to the rule as part of the TRY option is valid (existing user) or you may want to execute a real authentication transaction so that the rule is run with actual user data.


#3

i tried your implementation and it still did not work in my case. could you provide a snippet of the code process. i be most grateful. thanks


#4

Can you be more specific, have you performed a real authentication transaction or are you still using the try button?


#5

yes i have performed a real authentication transaction and my webapi was not hit to return the customId.i tried the implentation using postman and it was returned.i do not know why auth0 does not hit my api end point @jmangelo ,yes i have performed a real authentication transaction and my webapi was not hit to return the customId.i tried the implentation using postman and it was returned.i do not know why auth0 does not hit my api end point


#6

yes i have performed a real authentication transaction and my webapi was not hit to return the customId.i tried the implentation using postman and it was returned.i do not know why auth0 does not hit my api end point @jmangelo ,yes i have performed a real authentication transaction and my webapi was not hit to return the customId.i tried the implentation using postman and it was returned.i do not know why auth0 does not hit my api end point


#7

Given this requires a call to your API I cannot reproduce the full scenario locally; you should add console.log statements to the rule itself and then use the Webtask Real-time Logs extension to analyze the output and see if you get more information about the code path being executed in the rule.


#8

hello i tried it again and it worked . although i was looking through the code and i expected auth0 to post the user details to me but it all came to my api as null @jmangelo


#9

hello i tried it again and it worked . although i was looking through the code and i expected auth0 to post the user details to me but it all came to my api as null @jmangelo


#10

@uviekelvin I am currently trying to get this to work and cannot figure out how to properly receive the user and context data into the web api. I have tried receiving params as strings, objects, but am unsure how to get the json data. How did you set up your server method to see the data Auth0 is sending?


#11

@uviekelvin I am currently trying to get this to work and cannot figure out how to properly receive the user and context data into the web api. I have tried receiving params as strings, objects, but am unsure how to get the json data. How did you set up your server method to see the data Auth0 is sending?


#12

Have in mind that the user and context data is somewhat flexible in terms of what properties exist for each user and then ASP.NET Wev API may be expecting a more static schema. You’ll either need to accept dynamic data on the API endpoint or pass only a fixed set of attributes so that they map exactly to what the API is defined to expect at compile time.


#13

BTW - Auth0 is hitting my API endpoint, and I can return a number that comes back properly. I just cannot retrieve the user or context properly…


#14