Post login action - trasnfer user role to the backend

Hi @almogco94,

Welcome to the Auth0 Community!

You will want to avoid calling the management API every time a user authenticates, as you will quickly run into the rate limit.

Try this code instead:

/**
 * @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://my-app.example.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
}

If this doesn’t solve it, can you share how you are trying to access the roles in your backend?