# I have a use case in which , we create organizations based on signed up users email domain

**URL:** <https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [August 29, 2023, 12:19pm UTC](https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736 "2023-08-29T12:19:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanketrannore](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sanketrannore](https://community.auth0.com/u/sanketrannore)\
**Post date:** [August 29, 2023, 12:19pm UTC](https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736/1 "2023-08-29T12:19:32Z")

</div>

```auto
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){
   
    management.organizations.create({name: user_domain}, function(err,response) {
      if (err) {
        return
      }
      var params = { id : response.id}
      var data = { members: [event.user.user_id] }
      management.organizations.addMembers(params, data, function (err) {
        if (err) {
            // Handle error.
        }
      })
    })
  }
  else{
    //pass
  }
};

```

---

<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:** [August 29, 2023, 9:37pm UTC](https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736/3 "2023-08-29T21:37:37Z")

</div>

Hi @sanketrannore,

Welcome to the Auth0 Community!

I have reviewed your Post-Login Action script and see that you are creating a new organization based on the user’s email domain.

You may encounter issues with creating a duplicate organization if a user with the same email domain tries to log in. Because of this, you should add a check to see if the organization already exists. If so, you can skip creation and add them directly to the organization.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![sanketrannore](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sanketrannore](https://community.auth0.com/u/sanketrannore)\
**Post date:** [August 31, 2023, 3:39am UTC](https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736/4 "2023-08-31T03:39:54Z")

</div>

Thank you @rueben.tiow , I have updated my code .

```auto
exports.onExecutePostLogin = async (event, api) => {

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

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

  const data = event?.user.email?.split('@')[1].split('.')[0]

  try {
    var params = {
      per_page: 100,
      page: 0
    };
    const orgs=await management.organizations.getAll(params)
    const userOrg=orgs.filter(orgData=>orgData.name.toLowerCase().includes(data?.toLowerCase()))
    console.log("userOrg",userOrg,userOrg.length)
    if(userOrg.length==0)
    {
      let res = await management.organizations.create({"name": data});
      var org_id = res.id;
      var params2 = { "id" : org_id}
      var data2 = { members: [event.user.user_id] }
      let res2 = await management.organizations.addMembers(params2,data2)
    }
    else
    {
      var org_id = userOrg[0].id;
      var params2 = { "id" : org_id}
      var data2 = { members: [event.user.user_id] }
      let res2 = await management.organizations.addMembers(params2,data2)
    }
  } catch (e) {
    console.log(e)
  }
};

```

Im able to create a organization , but when the user logs in or signs up  
users and isAuthenticated in useAuth0 hook is giving as undefined and false and getting error (Error: Invalid state  
at te.handleRedirectCallback)

---

<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:** [August 31, 2023, 9:28pm UTC](https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736/5 "2023-08-31T21:28:41Z")

</div>

HI @sanketrannore,

Thanks for following up.

The Action script looks good to me 👏. Now, could you please clarify if you logged in using the `loginWithRedirect()` function?

Reference: [Auth0ContextInterface | @auth0/auth0-react](https://auth0.github.io/auth0-react/interfaces/Auth0ContextInterface.html#loginWithRedirect)

Using the `loginWithRedirect()` function should set a random and secure `state` on your behalf and resolve the Invalid State error.

Thanks,  
Rueben

---

<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:** [September 14, 2023, 9:29pm UTC](https://community.auth0.com/t/i-have-a-use-case-in-which-we-create-organizations-based-on-signed-up-users-email-domain/114736/6 "2023-09-14T21:29:16Z")

</div>

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