# Pre registration Action does not add metadata

**URL:** <https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075>\
**Category:** Get Help\
**Tags:** meta, app\_metadata, actions\
**Created:** [April 2, 2022, 9:53pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075 "2022-04-02T21:53:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [April 2, 2022, 9:53pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075/1 "2022-04-02T21:53:51Z")

</div>

Can you please explain is my code right? It do not add default role (id in secrets)

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

  try {

    api.user.setUserMetadata("role", event.secrets.defaultRoleId);

  } catch (e) {

    console.error(e);

  }

};

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 4, 2022, 3:49pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075/2 "2022-04-04T15:49:06Z")

</div>

Hi @lonli.lokli,

I moved this to a new topic because it’s a seperate question.

It looks okay, are you seeing any errors? Can you try logging or debugging to see what is going on?

---

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [April 4, 2022, 9:37pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075/3 "2022-04-04T21:37:26Z")

</div>

> [@dan.woda](#):
>
> It looks okay, are you seeing any errors? Can you try logging or debugging to see what is going on?

I do not see any errorrs. I do not know how to debug - Test Custom action works.  
I do not see any logging information in Monitoring\Logs, while my action is part of Pre-User Registration flow

 ![image](https://global.discourse-cdn.com/auth0/original/3X/9/1/916487926b83355a76d3afd17f3ee0c452b7c021.png)

 ![image](https://global.discourse-cdn.com/auth0/original/3X/4/5/4570bd66a4123f1caaaa3044b3fc8006218c4d3a.png)

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 4, 2022, 9:42pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075/4 "2022-04-04T21:42:33Z")

</div>

Could you please DM me the name of your tenant?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 7, 2022, 3:24pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075/5 "2022-04-07T15:24:26Z")

</div>

Resolved this via DM.

Here is an action that will add a default role to user and the token, or will add existing roles to the token.

```auto
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
  const namespace = "https://YOUR_NAMESPACE";

  if (event.authorization && event.authorization.roles.length === 0) {
    const ManagementClient = require('auth0').ManagementClient;
    const auth0 = new ManagementClient({
      domain: event.secrets.DOMAIN,
      clientId: event.secrets.CLIENT_ID,
      clientSecret: event.secrets.CLIENT_SECRET,
      scope: 'read:roles update:users create:role_members',
    })

    const params = {id: event.user.user_id}
    const data = {'roles':[event.secrets.DEFAULT_ROLE_ID]}

    await auth0.assignRolestoUser(params,data,(err) => {
      if (err) {
        console.log('DefaultRoleActionError: ', err)
      }
    })

    api.idToken.setCustomClaim(`${namespace}/roles`, event.secrets.DEFAULT_ROLE_NAME);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.secrets.DEFAULT_ROLE_NAME);

  } else if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
};
/**
* Handler that will be invoked when this action is resuming after an external redirect. If your
* onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
// exports.onContinuePostLogin = async (event, api) => {
// };

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 5:15pm UTC](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075/6 "2022-12-21T17:15:03Z")

</div>


