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

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
  }
};

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

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

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)

1 Like

HI @sanketrannore,

Thanks for following up.

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

Reference: Auth0ContextInterface | @auth0/auth0-react

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

Thanks,
Rueben

1 Like

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