# Add user to organization from email domain

**URL:** <https://community.auth0.com/t/add-user-to-organization-from-email-domain/81785>\
**Category:** Get Help\
**Tags:** organizations\
**Created:** [April 14, 2022, 7:02pm UTC](https://community.auth0.com/t/add-user-to-organization-from-email-domain/81785 "2022-04-14T19:02:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pepin/32/33997_2.png) [@pepin](https://community.auth0.com/u/pepin)\
**Post date:** [April 14, 2022, 7:02pm UTC](https://community.auth0.com/t/add-user-to-organization-from-email-domain/81785/1 "2022-04-14T19:02:22Z")

</div>

We are looking into using organisations to setup authentication on a dashboard we provide to our business partners. Is it possible to have users automatically added to an organisation according to their email domain?

For example, we have organization1, organization2 and organization3. We want to allow users to login to our dashboard with google, and be be automatically assigned to organization1 if their email is @organization1.com, etc.

How can we achieve this?

Thanks

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 15, 2022, 1:16am UTC](https://community.auth0.com/t/add-user-to-organization-from-email-domain/81785/3 "2022-04-15T01:16:41Z")

</div>

Hi @pepin,

Welcome to the Auth0 Community!

I understand you would like to programmatically assign users to different organizations based on their email domain.

To do so, you could use the Management API Add member to Organization endpoint in an Auth0 Post-Login-Action. In the code, you will need to check if the user’s email matches a particular domain and assign them to the Organization accordingly.

Below is the code needed to accomplish this:

```auto
exports.onExecutePostLogin = async (event, api) => {
  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]
  if(user_domain == 'example.com'){
    var params = { id :'YOUR_ORG_ID'};
    var data = { members: [event.user.user_id] }
    management.organizations.addMembers(params, data, function (err) {
    if (err) {
        // Handle error.
    }
    });
  }
  else{
    //pass
  }
};

```

I have tested this and can confirm that it works.

Lastly, please see [How can I use the Management API in Actions?](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947) FAQ for more details.

Please let me know if you have additional questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 22, 2022, 10:48am UTC](https://community.auth0.com/t/add-user-to-organization-from-email-domain/81785/4 "2022-12-22T10:48:56Z")

</div>


