# Can't auto-assign role to a passwordless email signup

**URL:** <https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552>\
**Category:** Get Help\
**Tags:** passwordless-email-connection, connections\
**Created:** [January 10, 2024, 1:58pm UTC](https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552 "2024-01-10T13:58:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkease](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bkease/32/53831_2.png) [@bkease](https://community.auth0.com/u/bkease)\
**Post date:** [January 10, 2024, 1:58pm UTC](https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552/1 "2024-01-10T13:58:21Z")

</div>

Hello!

I’ve been previously using a Rule to auto-assign a role to a new user on their signup for passwordless. I created a new tenant and was dismayed to learn that I can’t re-use the Rule I was using for this, that I had to create a custom Action instead.

Looking through documentation and these forums, I found two approaches. My preferred approach would be to use a post-registration action:

```auto
const auth0Sdk = require("auth0");

exports.onExecutePostUserRegistration = async (event) => {
	const ManagementClient = auth0Sdk.ManagementClient;

	// This will make an Authentication API call
  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret
  });

	await management.users.assignRoles(
		{ id: event.user.user_id }, 
		{ roles: [event.secrets.roleId]}, 
		(error) => {
			if (error) {
        console.log(error);
			}
		}
	);
};

```

This worked for a test, but when running on a signup, the `event.user.user_id` is blank. To my knowledge, I can’t call the Management API to update a user without the user’s `id`.

Per some of the discussion here, I also tried the post-login flow.

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

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

  const management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret
  });

  const params = { id : event.user.user_id };
  const data = { "roles" : [event.secrets.roleId] };

  try {
    await management.assignRolestoUser(params, data);
  } catch (e) {
    console.log(e);
    // Handle error
  }
};

```

This also worked in a test, but when I did a signup with it deployed into the flow, this didn’t even run for a passwordless login.

What am I missing here? My company’s use of the Auth0 product depends on this flow working. Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 10, 2024, 8:14pm UTC](https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552/3 "2024-01-10T20:14:42Z")

</div>

Hi @bkease,

Welcome to the Auth0 Community!

Yes, it is recommended to use the Post-Login Flow for this case.

After reviewing your code snippet in your Post Login action script, I noticed that you are using the outdated method to assign roles to a user, specifically `management.assignRolestoUser`.

The correct method now for assigning roles to a user is `management.users.assignRoles()`, just as you did in your Post-User Registration Action script.

(Reference: [UsersManager | auth0](https://auth0.github.io/node-auth0/classes/management.UsersManager.html#assignRoles))

Could you please give that a try and let me know how it goes for you?

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![bkease](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bkease/32/53831_2.png) [@bkease](https://community.auth0.com/u/bkease)\
**Post date:** [January 11, 2024, 3:53pm UTC](https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552/4 "2024-01-11T15:53:34Z")

</div>

Thanks for the response! As I mentioned, the post-login flow is not running for me with the passwordless email login. The post-registration flow always runs, but the post-login flow never does.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 11, 2024, 10:28pm UTC](https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552/5 "2024-01-11T22:28:39Z")

</div>

Hi @bkease,

Thanks for the reply.

Have you made sure that your [Post-Login Action script was attached to the flow](https://auth0.com/docs/customize/actions/write-your-first-action#attach-the-action-to-a-flow)?

If so, you could include `console.log()` statements and monitor the logs using the [Real-time Webtask Logs Extension](https://auth0.com/docs/customize/extensions/real-time-webtask-logs).

Cheers,  
Rueben

---

<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:** [January 25, 2024, 10:28pm UTC](https://community.auth0.com/t/cant-auto-assign-role-to-a-passwordless-email-signup/124552/6 "2024-01-25T22:28:55Z")

</div>

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