# Create Organization for user on post-login action

**URL:** <https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187>\
**Category:** Get Help\
**Tags:** organizations-management-api, management-api\
**Created:** [June 28, 2023, 5:11pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187 "2023-06-28T17:11:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![artem2](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/artem2/32/45142_2.png) [@artem2](https://community.auth0.com/u/artem2)\
**Post date:** [June 28, 2023, 5:11pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/1 "2023-06-28T17:11:39Z")

</div>

Hey guys!

I have a use case of organizations.

The idea i’m trying to achieve is next:

- User logs/signups into my SPA app.
- on post-login action organization created from domain name of used email (if it’s not a public email)
- user being attached to an organization.

Tried to compose this code, but still have no luck.

```auto
const uuid4 = require('uuid4');

exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count > 1) {
    return
  } 
  const ManagementClient = require('auth0').ManagementClient;

  const management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.client_id,
      clientSecret: event.secrets.client_secret,
  });

  const user_domain = (event.user.email.split('@')[1]).split('.')[0]
  if(user_domain){
    const id = uuid4()
    management.organizations.create({id: id, name: user_domain}, function(err) {
      if (err) {
        return
      }
      var params = { id : id}
      var data = { members: [event.user.user_id] }
      management.organizations.addMembers(params, data, function (err) {
        if (err) {
            // Handle error.
        }
      })
    })
  }
  else{
    //pass
  }
};

```

Thank you!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 28, 2023, 5:42pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/2 "2023-06-28T17:42:12Z")

</div>

Hey @artem2 welcome to the community!

Thanks for sharing your code - I haven’t tried it myself yet, but are you getting any errors? Where is it breaking down?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 28, 2023, 6:29pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/3 "2023-06-28T18:29:27Z")

</div>

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 in`management.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`.

```auto
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.
      }
    });
});

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![artem2](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/artem2/32/45142_2.png) [@artem2](https://community.auth0.com/u/artem2)\
**Post date:** [June 29, 2023, 8:14am UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/4 "2023-06-29T08:14:34Z")

</div>

Hey @ty.frith thanks for sharing an answer.

I tried the sample of your code, but it’s still does not create an ORG nor attach user to it.

Organization strategy for application looks like this:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/4/3/43606bbc7c7b9a415ac1115fb9773350c1c12aeb.jpeg)

The action on newly created user is shown as ‘success’, but in reality nothing has been done:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/5/9/5915f7e682eb28ec7ef780494dfc2468864093b7.jpeg)

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.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/8/a/8a7a2e3442e96be0f418eeadf9f9741d0acbd4d2.jpeg)

What could be the reason there, and if I should implement this inside of my backend, instead of auth0 post-login actions?

Thank you!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 29, 2023, 11:04pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/5 "2023-06-29T23:04:48Z")

</div>

Hey @artem2 no problem, happy to help!

> [@artem2](#):
>
> 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![artem2](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/artem2/32/45142_2.png) [@artem2](https://community.auth0.com/u/artem2)\
**Post date:** [July 2, 2023, 2:42pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/6 "2023-07-02T14:42:26Z")

</div>

Hey!

Thank’s for answering 😃

Tested code related to `user_domain`, it passes perfectly.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/c/5/c5ea90ffec2616e2aeb17bda1beb07f960759619.jpeg)

Could it be some issues with `ManagementClient`, because I was struggling to find values for it.

Because the domain, client id and secret were used from SPA app.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 5, 2023, 11:37pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/7 "2023-07-05T23:37:41Z")

</div>

Hey @artem2 I apologize for the delayed response here!

> [@artem2](#):
>
> Could it be some issues with `ManagementClient`, because I was struggling to find values for it.
> 
> Because the domain, client id and secret were used from SPA app.

That could definitely be an issue - You’ll want to create an M2M app to use for the `ManagementClient`:

[https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947#h-1-create-a-machine-machine-application-for-the-action-4](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947#h-1-create-a-machine-machine-application-for-the-action-4)

---

<div class="post-metadata">

**Author:** ![artem2](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/artem2/32/45142_2.png) [@artem2](https://community.auth0.com/u/artem2)\
**Post date:** [July 10, 2023, 10:46am UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/8 "2023-07-10T10:46:16Z")

</div>

Hey @ty.frith !

Thank you for keeping engagement with my issue, really appreciate this 🙂

Luckily, the guess about M2M app requirement was valid and I was able to resolve this.

To sum it up for anyone who’s gonna google this:

1. Code is valid as of time of writting.
2. Use M2M application keys to be able to perform such action.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 10, 2023, 5:35pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/9 "2023-07-10T17:35:55Z")

</div>

Hey @artem2 thanks for following up with the community, glad you were able to get this sorted! 🚀

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 24, 2023, 5:36pm UTC](https://community.auth0.com/t/create-organization-for-user-on-post-login-action/110187/10 "2023-07-24T17:36:19Z")

</div>

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