# Lazy Migration with Organisations - Issue Assigning Legacy Users to Organisations

**URL:** <https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785>\
**Category:** Get Help\
**Tags:** automatic-migration, user-management\
**Created:** [December 17, 2024, 2:44pm UTC](https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785 "2024-12-17T14:44:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ammo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ammo/32/60168_2.png) [@ammo](https://community.auth0.com/u/ammo)\
**Post date:** [December 17, 2024, 2:44pm UTC](https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785/1 "2024-12-17T14:44:27Z")

</div>

Hello all,

We are currently rolling out Auth0 for our services, and our setup involves supporting both business users and personal users. This means:

1. Some of our customers belong to organisations that have their own custom Auth0 login pages.
2. Other customers do not belong to any organisation and authenticate via a generic login page accessible to all users.

We have chosen to use the **lazy migration** strategy. I’ve managed to configure the `get user` and `login` custom database scripts to work successfully, allowing users to migrate from Cognito into Auth0. I’ve also set up Actions to assign users to their correct organisation on their **first login** when they authenticate through the **generic login page**.

The issue arises with **legacy users** who log in via an **organisation-specific login page**. Here’s what happens:

- The `get user` and `login` scripts work as expected, and the user is migrated into Auth0.
- However, immediately after this, authentication fails because the user is not yet a member of the organisation.
- As a result, **post-login Actions** do not fire, making it impossible to assign the user to the correct organisation.

### What I’ve Tried

1. **Assigning the user to an organisation within the `login` script:** This didn’t work because the user does not yet have a `user_id` during the `login` script execution.
2. **Pre-User Registration Triggers:** I encountered similar issues since these triggers don’t suit this use case.
3. **Post-Registration Triggers:** These are asynchronous, so they also didn’t solve the problem.

### What I’m Looking For

I need a solution to achieve one of the following:

1. **Redirect a newly migrated user** (who attempts to log in via an organisation-specific login page) to the **generic login page**. This would ensure that post-login Actions can fire and assign the user to the correct organisation.
2. Alternatively, a way to **assign a user to an organisation** as soon as they are created (during the same flow as the custom database script execution). This would allow them to be both migrated and assigned to an organisation seamlessly.

I’ve searched the forums extensively but haven’t found a solution that works. Any guidance or suggestions would be greatly appreciated.

Thank you!

---

<div class="post-metadata">

**Author:** ![sumansaurav](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sumansaurav/32/70627_2.png) [@sumansaurav](https://community.auth0.com/u/sumansaurav)\
**Post date:** [December 17, 2024, 4:11pm UTC](https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785/2 "2024-12-17T16:11:30Z")

</div>

Hi @ammo,

Here is what I could think of a potential solution/idea that will help you lazy migrate user.

I Have drawn a simple use-case diagram to understand this better.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/a/4/a4a7ecb6108c014de4d44179b803a2f4be6e437f.png)

1. First, modify your `get_user` script to identify the user type and connection:

```auto
function getUser(email, callback) {
  getUserFromCognito(email)
    .then(async (cognitoUser) => {
      // Add metadata about the connection and user type
      const userData = {
        email: cognitoUser.email,
        user_metadata: {
          migrated_from_cognito: true,
          original_connection: connection.name,
          is_business_user: determineIfBusinessUser(cognitoUser)
        },
        // ... other user properties
      };
      
      callback(null, userData);
    })
    .catch(err => callback(err));
}

```

1. Then, modify your `login` script to handle different paths:

```auto
function login(email, password, callback) {
  authenticateAgainstCognito(email, password)
    .then(async (success) => {
      if (success) {
        const isBusinessUser = user.user_metadata.is_business_user;
        const connectionName = connection.name;
        
        if (isBusinessUser && connectionName.startsWith('org-specific')) {
          // Business user trying to login through org-specific page
          // Redirect to generic login with special parameters
          callback(null, {
            redirect_url: `https://your-domain.auth0.com/login?` +
                        `migration=true&` +
                        `original_connection=${connectionName}&` +
                        `business_user=true`
          });
        } else {
          // Personal user or already on generic login
          callback(null, user);
        }
      } else {
        callback(new WrongUsernameOrPasswordError(email));
      }
    })
    .catch(err => callback(err));
}

```

1. Add a new Action to handle the organization assignment:

```auto
// Post-login action
exports.onExecutePostLogin = async (event, api) => {
  if (event.user.user_metadata.migrated_from_cognito) {
    const managementClient = await getManagementClient();
    
    if (event.user.user_metadata.is_business_user) {
      // Business user flow
      const orgId = determineOrgFromConnection(
        event.user.user_metadata.original_connection
      );
      
      await managementClient.users.assignOrganizations({
        id: event.user.user_id,
        organizations: [orgId]
      });
      
      // If came from redirect, send back to org login
      if (event.request.query.migration === 'true') {
        api.redirect.sendUserTo(
          event.user.user_metadata.original_connection
        );
      }
    } else {
      // Personal user flow
      await managementClient.users.assignOrganizations({
        id: event.user.user_id,
        organizations: ['default-personal-org']
      });
    }
  }
};

```

1. Add a Universal Login page customization to handle the migration flow:

```auto
// In your Universal Login page
async function handleLogin(event) {
  if (event.query.migration === 'true') {
    // Show migration-specific UI if needed
    await handleMigrationFlow({
      business: event.query.business_user === 'true',
      originalConnection: event.query.original_connection
    });
  }
  // ... rest of your login logic
}

```

This solution:

1. Identifies user type during migration
2. **Redirects business users to the generic login temporarily**
3. Assigns appropriate organization membership
4. Returns business users to their org-specific login page

Hope this helps!

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [December 17, 2024, 5:17pm UTC](https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785/4 "2024-12-17T17:17:36Z")

</div>

Hi all! Thanks @sumansaurav for the detailed reply for @ammo!

@ammo, please be sure to share if this solution works for you 🙂

Thanks,

Mary Beth

---

<div class="post-metadata">

**Author:** ![ammo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ammo/32/60168_2.png) [@ammo](https://community.auth0.com/u/ammo)\
**Post date:** [December 17, 2024, 5:53pm UTC](https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785/5 "2024-12-17T17:53:55Z")

</div>

Thanks, @sumansaurav for the quick and very detailed response. I will give it a try and update this topic with how it went 🙇‍♂️

---

<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:** [December 31, 2024, 5:54pm UTC](https://community.auth0.com/t/lazy-migration-with-organisations-issue-assigning-legacy-users-to-organisations/163785/6 "2024-12-31T17:54:05Z")

</div>

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