How to make auth0 api request in Post-User Registration hooks?

I want to create a Post-User registration hook that sends an email to certain users of the application. The first step I need is to retrieve the users from the /api/v2/users endpoint. I don’t currently see how to get and pass the authorization token to the /api/v2/users endpoint from inside the hooks endpoint. Is there something I need to configure to get an accessToken from inside the hook? Is the accessToken already passed in somewhere and I can retrieve it? Are there any examples of making auth0 api requests from inside a hook? Any help would be appreciated!

Hi @mavendano,

You could use the Auth0 Node.js client library in a hook by adding it as an NPM module (search for auth0 in the add module section, under the spanner) . This will handle most of the work for you and allow you to call the various management API endpoints. You can also store the clientID and clientSecret of the application you are using to make the API requests as Hook secrets, as unlike rules, there is no access token for the API available. Below is a basic example of how this could work:

module.exports = function (user, context, cb) {
  // Perform any asynchronous actions, e.g. send notification to Slack.
  var ManagementClient = require('auth0').ManagementClient;
  var auth0 = new ManagementClient({
     domain: context.webtask.secrets.domainName,
     clientId: context.webtask.secrets.clientID,
     clientSecret: context.webtask.secrets.clientSecret,
     scope: 'read:users'
   auth0.getUsersByEmail('', function(err, users) {
  if (err) {
    // handle error.

Alternatively you could do something similar in a rule like mentioned here, and only run it for users who have not logged in more than once using the context.stats.loginsCount or in the absence of some other custom metadata flag you add after the emails have been sent. In rules there is already an access token available so you would just need to pass that into the client initialisation

1 Like

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