I recently researched how to best add a default role to a new user who is using a particular client. Specifically, a user that either just signed up, or was imported from a legacy database. IOW, the user just logged in or signed up for the first time, and is doing so using client “Foo”
To get this behavior to occur, I ended up doing the following, which appears to work well, but is not as elegant as I would like. I used rules (not Hooks) to implement this:
Rule1 - auth0-authorization-extension (auto-added when I installed the authorization extension). This rule ensures that any existing groups/roles/permissions are present in the app_metadata
Rule2 - Client Foo - set default role on new user:
- this is a custom rule
- check to see if client is “Foo”, of not, exit rule
- if user does not have the default role (in app_metadata), call Auth0-authorization-extension to add the role
- if role added successfully, “context.roleInitialized = true”
- exit
Rule3 - Client Foo - if new role added, re-initialize authorizations
- check to see if client is “Foo”, of not, exit rule
- if context.roleInitialized !== true, exit rule
- re-run all the generated JS code that is in “auth0-authorization-extension”. IOW, I just manually copied this rule from above, and added an initial “if()” to make sure the client is Foo & new role was just added
So, rather than doing this logic in a webhook (which I thought made better sense conceptually, and was cleaner), I did it in rules. The reason I did not do it in the “Pre User Registration” hook was that this hook (per the doco) does not know what client initiated the user signup or import. There may be other reasons why this hook would not work, but that initial reason (not knowing the client) was my signal to use rules instead.
To be clear, Rule3 is there so that the (new) role & corresponding permissions will be in the generated access_token. Without Rule3, the user is ‘added to the role’, but the access_token doesn’t have the role/permissions (which is the whole point so to speak)
Any advice as to a better way?