Auth0 Home Blog Docs

Auth0 metadata updating roles for a user using node.js

auth0
api
roles
user-metadata

#1

Hi All, I’m working on a project. I can manually add a role to a user via the interface but would like to do this via code. So, for instance when a user successfully signs in and enters their contact details I can then automatically add a role to their user profile like “customer” or “supplier” etc. Has anyone done this yet? I’m using node with angular and typescript. Thanks!


#2

:wave: @james.leith.aus you can add roles via a Rule, an example can be found here, which may be an adequate solution for you. Let me know if this is what you are looking to do!


#3

Hi Kim, thanks for this. I did that with the @companyname.com to grant them the host admin rights for instance. If though I wanted to people to complete a sign-up when they logged in which would make one a customer and one a supplier. I was hoping that at the end of the sign-up I could write back to auth0 and say “hey auth0, can you add the ‘customer’ rule to this new customers user profile” or “can you add the ‘supplier’ rule to this new supplier’s user profile” etc. I may have over complicated my expectations of Auth0 for a solution. It happens now and then :slight_smile: It just works in well with your IsAdmin solution, because I could apply similar IsCustomer, IsSupplier type options. Thanks for your blog by the way it’s been super helpful!


#4

Hey James, not a problem! I’m probably the first person to over complicated things :slight_smile: I’m glad the solution worked. Let me know if we can help with anything else!


#5

Hi Kim, You wouldn’t know how to write back from the node app to Auth0 would you? I think it’s through the management api, then the app_metadata. I confused the Kim’s by the way (Kim Maida who wrote a great Auth0 blog) lol

The scenario is that I would have the admin, customer and supplier roles setup in Auth0. We can manually through the gui set the admin roles but because we could have hundreds of customers signing up I was trying to automate it. So the customer or supplier would sign-in, authenticate via Auth0 and the register inside the app. When we push the registration to the db I also wanted to push to auth0 to assign a supplier or customer rule to that user on Auth0; based on what they registered as in the app. Like I said, I do like to complicate things.


#6

hehe, no problem. I’ll let Kim know :slight_smile:

We could call the Auth0 Management API at a later point to assign their role in the app_metadata using https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id
Would this work for you?


#7

Hi Kim, that works and it does allow me to do it by code … but it does though replace the “admin” rule for instance with the “customer” rule, instead of keeping both. Is there a trick you know that will keep the predecessor also? Sorry for the trouble. James


#8

Actually Kim, if I can’t append a rule to an existing rule so have … { “roles”: “admin”, “customer” } … then I can probably just work some if, else type statement in there on the app side. Nice though to be able to append if that’s possible. J