Not getting roles

When i’m trying to get user roles in access token it does not work for me
what i’m doing wrong can anyone help me to figure out1
This is my action code

exports.onExecutePostLogin = async (event, api) => {
    const ManagementClient = require("auth0").ManagementClient;
     const management = new ManagementClient({
        domain: event.secrets.domain,
        clientId: event.secrets.clientId,
        clientSecret: event.secrets.clientSecret,
    const userRoles = await management.users.roles(event.user.user_id);

    api.accessToken.setCustomClaim("roles", userRoles);

error i’m getting
management.users.roles is not a function

Hi @pankaj.y ,

Thanks for sharing!

You’re missing namespace identifier in your custom claim + you do not need to use the ManagementClient here to get user roles, will be good to use event.authorization.roles. Actions Triggers: post-login - Event Object

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://something_you_choose';
  if (event.authorization) {
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);

Please let us know if that works for you!

1 Like

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