Looking through this again - You won’t be able to assign an id to the newly created organization. You can assign the domain as the name, but will need to omit the id from creating the organization and then use the id in the success response inmanagement.organizations.addMembers().
This code worked for me - Of course it will need tweaking to fit your use case with the domain and event.user.user_id.
const ManagementClient = require('auth0').ManagementClient;
const management = new ManagementClient({
domain: my_auth0_domain,
clientId: xxx,
clientSecret: xxx
});
const data = {"name": "org28"}
management.organizations.create(data, function (err, response) {
if (err) {
// Handle error.
}
//org created
var org_id = response.id
var params = { id : org_id}
var data = { members: [ 'auth0|xxxx' ] }
management.organizations.addMembers(params, data, function (err, response) {
if (err) {
// Handle error.
}
});
});
Even checking the logs after, I can see that the other action, which updates user app metadata has been executed (API Operation “Update User”), but nothing related to organization has been done.
Hmm that’s interesting - I see both API operations in my logging (create org and assign member) using the following Action. Again it’s a bit more simplified version of yours, primarily skipping the domain bit and just hardcoding the org name:
const ManagementClient = require('auth0').ManagementClient;
exports.onExecutePostLogin = async (event, api) => {
const management = new ManagementClient({
domain: event.secrets.AUTH0_DOMAIN,
clientId: event.secrets.CLIENT_ID,
clientSecret: event.secrets.CLIENT_SECRET
});
const data = {"name": "test_org_test"}
const user = event.user.user_id
console.log(`Here's the user ${user}`)
management.organizations.create(data, function (err, response) {
if (err) {
// Handle error.
}
//org created
var org_id = response.id
var params = { id : org_id}
var data = { members: [ user ] }
management.organizations.addMembers(params, data, function (err, response) {
if (err) {
// Handle error.
}
});
});
I’m wondering if it could be breaking down at your user_domain related code or another Action could be causing issues with this one.