# Need help with setting up a Post user registration action

**URL:** <https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [June 28, 2023, 8:55am UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144 "2023-06-28T08:55:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdmiralBot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/admiralbot/32/48043_2.png) [@AdmiralBot](https://community.auth0.com/u/AdmiralBot)\
**Post date:** [June 28, 2023, 8:55am UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/1 "2023-06-28T08:55:21Z")

</div>

- Failed Post User Registration Hook

- Description

ArgumentError on post-user-registration: Must provide a domain

I keep getting this error even though I have provided the domain of my application in my action

and I am using a modified version of the code given in this blog post:

> [@Adding roles to users post-registration using actions](https://community.auth0.com/t/adding-roles-to-users-post-registration-using-actions/75671):
>
> Hello, I’m pretty new to auth0 and what i’m trying to achieve is add specific roles that i have already created through the auth0 dashboard and assign them to certain users based on their email domain name I used this thread as a reference and followed it step by step: [How can I use the Management API in Actions?](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947) I actually tried to pull it off in the post-registration flow as well as in the post-login flow and neither worked for me Here’s what my action looks like exports.onExecutePostUserR…

Please let me know how I can fix this

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 28, 2023, 5:31pm UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/3 "2023-06-28T17:31:51Z")

</div>

Hi there @AdmiralBot!

Do you mind sharing your exact Action code here so we can take a look and see what might be the issue? I’d like to try and reproduce it as closely as possible.

Thanks!

---

<div class="post-metadata">

**Author:** ![AdmiralBot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/admiralbot/32/48043_2.png) [@AdmiralBot](https://community.auth0.com/u/AdmiralBot)\
**Post date:** [July 3, 2023, 5:23am UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/4 "2023-07-03T05:23:28Z")

</div>

hey I am attaching my code here please let me know what I need to fix

exports.onExecutePostUserRegistration= async (event, api) =\> {  
const namespace = “[https://myappname.us.auth0.com](https://myappname.us.auth0.com)”;

const ManagementClient = require(“auth0”).ManagementClient;

const management = new ManagementClient({  
domain: event.secrets.domain,  
clientId: event.secrets.clientId,  
clientSecret: event.secrets.clientSecret,  
scope: “read:roles create:roles update:roles”,  
});

const defaultRole = { id :‘rol\_R0LEZGNx7GPlmKXe’};  
var data = { “users” : [event.user.user\_id]};

try {  
if (event.authorization){  
api.idToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole);  
api.accessToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole);  
await management.roles.assignUsers(defaultRole, data);  
}  
} catch (e) {  
console.log(e);  
}

};

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 3, 2023, 11:37pm UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/5 "2023-07-03T23:37:54Z")

</div>

Thanks for sharing your code!

This is a bit trickier than I expected given a couple things:

- Post registration actions do not have access to the `event.authorization` object and therefore this needs to be broken out into a Post-login action.

- You’ll need to introduce login into the Post-login action in order to account for the fact that you want to include the `event.secrets.defaultRole` if it’s the user’s first login and `event.authorization.roles` if it’s any subsequent login. The reason being the role assigned in the Post-registration action will not be available via `event.authorization.roles` on first login.

Here’s a Post-registration and Post-login action that in unison are working for me:

**Post User Registration:**

```auto
exports.onExecutePostUserRegistration = async (event, api) => {

const namespace = "https://myappname.us.auth0.com";

const ManagementClient = require("auth0").ManagementClient;

const management = new ManagementClient({
domain: event.secrets.domain,
clientId: event.secrets.clientID,
clientSecret: event.secrets.clientSecret,
scope: "read:roles create:roles update:roles"
});

const params = { id : event.user.user_id};
var data = { "roles" : ['your_default_role_id']};

management.assignRolestoUser(params, data, function (err) {
  if (err) {
    // Handle error.
    console.log(err)
  }
  console.log(`User ${event.user.email} successfully assigned default role.`)
  // User assigned roles.
});
};

```

**Post Login:**

```auto
exports.onExecutePostLogin = async (event, api) => {
  
const namespace = "https://example.com"
  
//if this is the users first login, use the role hardcoded in event.secrets.defaultRole
 if (event.stats.logins_count == 1) {
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole ),
    api.idToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole )
  } else {
  //if this isn't the users first login, assign roles from event.authorization. 
  if (event.authorization) {
    console.log(event.stats.logins_count)
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles ),
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles )
    }
  }
};

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![AdmiralBot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/admiralbot/32/48043_2.png) [@AdmiralBot](https://community.auth0.com/u/AdmiralBot)\
**Post date:** [July 4, 2023, 5:17am UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/6 "2023-07-04T05:17:28Z")

</div>

hey thanks for the response. So i will need to use 2 actions here, one in the post registration flow and one in the post login flow am I correct??

Also it is showing this error for the post login action:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/b/3/b34f94dd62a0505cd8d291b25af50b7ee2ca2aba.png)

---

<div class="post-metadata">

**Author:** ![AdmiralBot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/admiralbot/32/48043_2.png) [@AdmiralBot](https://community.auth0.com/u/AdmiralBot)\
**Post date:** [July 6, 2023, 5:05am UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/7 "2023-07-06T05:05:19Z")

</div>

hey there @ty.frith is there any update??, the code you shared also shows the same error

 ![image](https://global.discourse-cdn.com/auth0/original/3X/5/8/58503a38fa3333b1320641d16b75e1abe492351b.png)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 6, 2023, 7:41pm UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/8 "2023-07-06T19:41:17Z")

</div>

Hey @AdmiralBot - That error looks to be related to a Post User Registration _Hook_ as opposed to a Post User Registration _Action_. Are you able to confirm whether or not that’s the case?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 6, 2023, 7:43pm UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/9 "2023-07-06T19:43:11Z")

</div>

> [@AdmiralBot](#):
>
> Also it is showing this error for the post login action:
> 
> ![image](https://global.discourse-cdn.com/auth0/original/3X/b/3/b34f94dd62a0505cd8d291b25af50b7ee2ca2aba.png)

You’ll need to add `defaultRole` to the actions `events.secrets` in order to resolve these errors - This is required for an initial login by a user as the role is not yet available in `event.authorization.roles`.

---

<div class="post-metadata">

**Author:** ![AdmiralBot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/admiralbot/32/48043_2.png) [@AdmiralBot](https://community.auth0.com/u/AdmiralBot)\
**Post date:** [July 7, 2023, 11:12am UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/10 "2023-07-07T11:12:47Z")

</div>

How do i add default role to event.secrets??

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 7, 2023, 11:41pm UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/11 "2023-07-07T23:41:03Z")

</div>

In this example, I am just using the string `defaultRole` role in `event.secrets` and adding it as a custom claim to tokens for the user’s first login _only_. Once a user logs in again, the `defaultRole` is added via `event.authorization.roles` - Using the `defaultRole` via `event.secrets` is acting as a sort of placeholder in order to have the role in tokens on first login.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 21, 2023, 11:41pm UTC](https://community.auth0.com/t/need-help-with-setting-up-a-post-user-registration-action/110144/12 "2023-07-21T23:41:04Z")

</div>

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