Management API problems NodeExpress

Hi, I’m hoping someone can please help. I’m really stuck with how to set new metadata with the Management API in vanilla nodejs/express (no React/Angular etc).

I saw this answer about getting a token but know there is also a way to automatically get tokens.

I’m particularly stuck with where this code is meant to go? My use case is very simple, I just want a user to be able to click a fill in a 1 field form and update their metadata with the result.

Where does that code go (from the example)? In app.js? In a controller? In the template?

If someone could just very simply (I’m a beginner!) explain this simple use case I know I will be fine doing more actions from there.

Right now I have an app.js, which uses a router.js, which forwards to a userController, which at the moment sends the user profile information to a Pug template at localhost:3000/user like this :

exports.user = (req, res) => {
    
    res.render('user', {
        user: req.oidc.user,
    });
};

Where it succesfully displays. If someone could explain very simple steps to use vanilla Express/NodeJS to simply add/read metadata from a user profile that would be amazing.

I have installed the Node auth0 library.

Thank you!!!

Hi @nc14,

Thanks for reaching out to the Auth0 Community!

I understand that you are trying to use the Management API to update your user’s user_metadata on your app.

To do so, you will need to first get a Management API Token using the client_credentials grant, which will programmatically request for tokens.

Once you have the token, you can use it to make requests to the Management API Update a user endpoint.

Lastly, you may find the updateUserMetadata reference useful.

Please let me know if you have any questions.

Thank you.

Hi thanks for this - I can now successfully get a token… can you please explain in more detail how I do this…

Once you have the token, you can use it to make requests to the Management API Update a user endpoint.

Hi @nc14,

Thank you for your response.

You could use the ManagementClient to call the updateUserMetadata method to accomplish this in your Node Express App. See below.

var ManagementClient = require('auth0').ManagementClient;

var management = new ManagementClient({
  token: 'YOUR_API_V2_TOKEN',
  domain: 'YOUR_DOMAIN.auth0.com'
});

var params = { id: USER_ID };
var metadata = {
  address: '123th Node.js Street'
};

management.updateUserMetadata(params, metadata, function (err, user) {
  if (err) {
    // Handle error.
  }

  // Updated user.
  console.log(user);
});

Please let me know if there’s anything else I can do to help.

Thank you.

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