Auth0 Home Blog Docs

Rule using node-auth0, AuthenticationClient is not a constructor

Hi,

We are trying to create a rule to assign a default role to every new user. In order to assign this role we are using the node-auth0 library. Using the code from the node-auth0 documentation we are encountering errors:

ERROR: AuthenticationClient is not a constructor

Here is the complete code of our rule:

function (user, context, callback) {
  if (!context.authorization || context.authorization.roles.length === 0) {
    if (user.email && user.email_verified) {
      var AuthenticationClient = require('auth0').AuthenticationClient;
 
      var auth0 = new AuthenticationClient({
        domain: '{DOMAIN}.auth0.com',
        clientId: configuration.MGMT_API_ID,
        clientSecret: configuration.MGMT_API_SECRET
      });
      
      auth0.clientCredentialsGrant(
        {
          audience: 'https://{DOMAIN}.auth0.com/api/v2/',
          scope: 'read:roles update:users'
        },
        function(err, response) {
          if (err) {
            console.log(err);
          }

          console.log(response.access_token);

          var ManagementClient = require('auth0').ManagementClient;
 
          var management = new ManagementClient({
            token: response.access_token,
            domain: '{DOMAIN}.auth0.com'
          });

          var params =  { id: user.user_id };
          var data = { 'roles': [ '{ROLE_ID}' ] };

          management.assignRolestoUser(params, data, function (err) {
            if (err) {
              console.log(err);
            }
          });

          if (!context.authorization) {
            context.authorization = {};
            context.authorization.roles = [];
          }
        
          context.authorization.roles.push("{ROLE}");

          callback(null, user, context);
        }
      );
    }
  }
}

I first tried using the ManagementClient instead of the AuthenticationClient but the same error is showing. Is there some special steps to do in order to be able to use the node-auth0 library in a rule?

Hi @paul.queruel,

Thanks for joining us in the Community.

Take a look at this FAQ:

Furthermore, check out this resource to see which modules are available in rules:
https://auth0-extensions.github.io/canirequire/

Please let me know if this works/doesn’t work for you.

Thanks,
Dan

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