Add new user to role

Hi,

I would like to add all new users created/registered in a specific connection (database) to a specific role the was created in Auth0 Dashboard (Authorisation Core). What would be the best way to achieve this?

To me, the most natural place to do this would be using a Post User Registration Hook. Can it be done? How, in that case? Any other ideas?

I was also thinking about using rules but it doesn’t seem like the correct place as then I would first need to check if the user is in the role and if not, add it. Feels like this is unnecessary to do on every login as it might slow things down…

Thank you in advance!

-Mattias

1 Like

Hi @mattias.skog,

Welcome to the Auth0 Community!

Can you take a look at this FAQ and see if it solves the problem?

Thanks,
Dan

1 Like

Hi Dan,

Thank you for the link! I tested that bur for some reason it didn’t work, will have to look into it more today. However, still feels like this is not the correct way to do it because this requires the user to log in ones before he is put into the role.

What if I would like to search for all users in a specific role from an external application using the Management API? I can’t find that specific user until he has logged in ones…or, can I perform a search based on a given connection? Don’t think it’s possible through the API, or is it?

-Mattias

Ah yes, because you are trying to add the roles on creation, which may not include a login. I overlooked that, apologies.

It sounds like for your use case the post user reg hook will be the best place to do this, like you initially mentioned.

You should register the hook as a M2M app, give the app management API permissions, then you can make the call directly from the hook. It will be similar to the rule I linked.

Does that make sense?

Let me know if you need help on specific parts of it.

Thanks,
Dan

Thanx Dan for the response! It makes sense…some code example for the hook would be welcome.

@mattias.skog

This should work for you.

module.exports = function (user, context, cb) {
  console.log('Starting add-role-to-new-user...');
  
  var ManagementClient = require('auth0@2.19.0').ManagementClient;
  var management = new ManagementClient({
    domain: '{DOMAIN}',
    clientId: '{CLIENT ID}',
    clientSecret: '{CLIENT SECRET}',
    scope: 'update:users'
  });
  
  var params =  {id : 'auth0|' + user.id};
  var data = {"roles" :["{ROLE ID}"]};
  
  management.assignRolestoUser(params, data, function (err) {
    if (err) {
      console.log(err);
    }
    console.log('Role added to new user.');
  });

  cb();
};

You will need to register a new M2M app as your hook, and give it update:users permissions in the management api. Then find your role id using the management api explorer get roles endpoint. Also be conscious of management api rate limits, you may want to build in some error handling if you expect a high volume. Look here for guidance:

Thank you! This was exactly what I was looking for…

2 Likes

Great! Glad it worked for you.

1 Like

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