Prevent Users from Joining Multiple Organizations

In my auth0 I have multiple organizations and users specifics to each of the organizations. The issue is that I would like to prevent a user to be added in an organization if he is already part of another organization.
Is there a rule that can resolve this issue or something that can be added in the user metadata?
I have the following rule but it is not working:

function preventMultipleOrgs(user, context, callback) {
  // TODO: implement your rule
  const namespace = '';
  const orgId =;

  // Skip this rule if no organization is provided
  if (!orgId) {
    return callback(null, user, context);

  // Fetch the user's current organizations
    const ManagementClient = require('auth0').ManagementClient;
    const management = new ManagementClient({
    token: auth0.accessToken,
    domain: auth0.domain

  management.users.getUserOrganizations({ id: user.user_id })
    .then(organizations => {
      // Check if the user is already a member of another organization
      const hasOtherOrg = organizations.some(org => !== orgId);

      if (hasOtherOrg) {
        // Prevent the user from joining a new organization
        console.log('User is already a member of another organization.');
      } else {
        // Allow the user to join the organization
        return callback(null, user, context);
    .catch(err => callback(new Error('Error fetching user organizations.')));

Is there something I am doing wrong?

Hey there @sandT!

Out of curiosity, how is a theoretical user that is to be blocked by this code being added to an organization?

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