Working example of updating metadata in Nodejs?

I have a NodeJS/Express backend. I’m building a React SPA which creates groups of users with a single group admin. If a new user creates their own group, then I mark them as an admin in my own user DB. I also want to leverage an Auth0 role to control permissions. I have a structure for this in both front and backends which works - but I currently manage role assignment only through the console. What I want instead is to include code in my backend that communicates with the Auth0 mgmt api during the New Group process and assigns the admin role to the user in question.

I found this guidance: Manage Metadata Using the Management API

But it’s still not clear to me how to actually use this in my backend operationally. All I need if a single working Github example of mgmt API use in a NodeJS and I can probably figure it out - but I can’t find any working examples anywhere. Does anyone know of an example I can reference to see Node JS mgmt API communication in action, most especially an example which updates user or app metadata?

Hi @athome176,

Thanks for reaching out to the Auth0 Community!

I understand that you need some help with a working example of using the Management API to update a user’s metadata in Node.js

Here is an example:

var axios = require("axios").default;

var options = {
  method: 'PATCH',
  url: 'https://YOUR_DOMAIN/api/v2/users/user_id',
  headers: {authorization: 'Bearer ABCD', 'content-type': 'application/json'},
  data: {user_metadata: {addresses: {home: '123 Main Street, Anytown, ST 12345'}}}
};

axios.request(options).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
  console.error(error);
});

Let me also add that you can find this same example in the doc you shared and clicking on the Node.js tab:

I hope this helps!

Please let me know if I can help you with anything else.

Thanks,
Rueben

1 Like

Thanks for the quick reply Reuben! I saw that example. The problem is that I have no idea how to actually use this in practice. If there were even a single working sample app that used this operationally, I could look at the sample app and figure it out. Does Auth0 have a sample app that uses this anywhere?

1 Like

Hi @athome176,

Thank you for your response.

I recommend reading our Auth0 Express SDK Quickstart documentation.

The documentation has an example application and a guide to using our Express SDK.

Does that help?

Thanks,
Rueben

1 Like

Thanks for the links Rueben. Unfortunately, none of that helps in this case as I do not see any examples of updating metadata via Auth0 mgmt api from the backend. I will keep digging through documentation and examples to see if I can find something.

Hi @athome176,

Thank you for your reply.

You should be able to embed the code snippet I shared in my initial post into your backend code to make a call to the Management API to update the user’s user_metadata.

Were you having any issues getting that implemented?

Thanks,
Rueben

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