Unable to assign role to user

Hi,
I’m currently working on a website and want to assign a role to a user from code. (It’s my first project using Auth0)
The problem is that I always get the response: statusCode: 401 error: Unauthorized message: "Missing Authentication (and a bunch of other stuff that probably isn’t relevant), even tough I use the same api token that worked, when using the api explorer/tried getting a new one every time I use. (The cURL command that the api explorer gives me, also doesn’t work).
that is my code:

`var options = {
    method: 'POST',
    url: 'https://TENANT.eu.auth0.com/api/v2/roles/ROLE_ID/users',
    header: {
        'content-type': 'application/json',
        authorization: 'Bearer ACCESS_TOKEN',
    },
    data: {
        "users": [userID]
    }
};
axios.request(options).then(function (response) {
    console.log(response.data);
}).catch(function (error) {
    console.error(error);
});`

I also tried a bunch of variations of that or assigning permissions instead of roles and everything gave me 401.
Thanks in advance!

Hi @Rerebla,

Welcome to the Community!

It looks like you need to use the property headers instead of header axios - npm

var options = {
    method: 'POST',
    url: 'https://TENANT.eu.auth0.com/api/v2/roles/ROLE_ID/users',
    headers: { // <-- UPDATE THIS
        'content-type': 'application/json',
        authorization: 'Bearer ACCESS_TOKEN',
    },
    data: {
        "users": [userID]
    }
};
axios.request(options).then(function (response) {
    console.log(response.data);
}).catch(function (error) {
    console.error(error);
});

Thanks for the quick reply!
How in the world could I have overlooked that?
Anyways, now it works. But now I’m wondering if that is a paid feature, because I still have the trial and in the comparison it says that “Role Management” is not included in the free plan.


Is there maybe an option to skip the trial or something?
Thanks again for the solution!

Unfortunately, I don’t think you can opt-out of the trial.

I have noticed that in my free plan tenant I am able to add roles and assign them to users. I’m asking around to see what the restrictions are around role management for the free plan. I will let you know what I find!

After talking with my team, I realized that it would be best to keep track of roles in a custom rule instead of using the core authorization if you are on the free plan.

Here is an example of adding role information in a rule using app metadata: Manage Metadata with Rules

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