# Re-authenticate the user with a new organization context

**URL:** <https://community.auth0.com/t/re-authenticate-the-user-with-a-new-organization-context/198993>\
**Category:** Get Help\
**Tags:** connection-provisioning-flows, connections\
**Created:** [April 15, 2026, 5:01pm UTC](https://community.auth0.com/t/re-authenticate-the-user-with-a-new-organization-context/198993 "2026-04-15T17:01:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![binh](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/binh/32/63335_2.png) [@binh](https://community.auth0.com/u/binh)\
**Post date:** [April 15, 2026, 5:01pm UTC](https://community.auth0.com/t/re-authenticate-the-user-with-a-new-organization-context/198993/1 "2026-04-15T17:01:48Z")

</div>

In my application (SPA React), I use Moodle to authenticate my users by creating a custom social connection in Auth0 named “moodle-oauth2” with a corresponding organization “Moodle shell org“ with auto-provisioning turned on.

The flow is described as follows:

- User lands on my application
- auth0-react sdk logs the user in with connection “moodle-oauth2“
- The user is redirected to Moodle for login, then given consent to Auth0
- Finally, the user logs in to my application with “Moodle shell org”

The desired result is that in the post-login action, I want to assign the user to the corresponding organization based on their email domain, and remove the user from the Moodle shell org. Additionally, I want to authenticate the user into the new organization instead of the Moodle shell org.

I know how to assign and remove users from an organization. After this step, how can I re-authenticate them to the new org context?

Here is my script:

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

  const moodleAcademyOrgId = "org_X6b3smoodle"

  // Only assign user's organization when the user comes from moodle
  if (event.connection.name !== "moodle-oauth2") {
    return
  }

  const email = event.user.email
  if (!email) return

  const isFirstLogin = event.stats.logins_count == 1
  if (!isFirstLogin) return

  // Do nothing if there is already organization context
  if (event.organization) {

    if (event.organization.id !== moodleAcademyOrgId) {
    console.log("Skip because already in organization context", event.organization.display_name)
      return
    }

  }

  const managementApi = new ManagementClient({
    domain: event.secrets.DOMAIN,
    clientId: event.secrets.CLIENT_ID,
    clientSecret: event.secrets.CLIENT_SECRET,
  })

  try {

    const orgId = "org_dedicated_org"

    // Hardcode: assign user to a dedicated org
    await managementApi.organizations.members.create(orgId, {
        members: [event.user.user_id]
      });
    console.log("Successfully assign user to organization", event.user.user_id, orgId)

    await managementApi.organizations.members.delete(moodleAcademyOrgId, {
        members: [event.user.user_id]
      });
    console.log("Successfully remove user to organization", event.user.user_id, moodleAcademyOrgId)
    
    // How to re-authenticate the user with the new org context
    

  } catch (err) {
    console.log("Organization assignment error:", err)
  }

};

```

---

<div class="post-metadata">

**Author:** ![remus.ivan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/remus.ivan/32/68771_2.png) [@remus.ivan](https://community.auth0.com/u/remus.ivan)\
**Post date:** [April 21, 2026, 3:49pm UTC](https://community.auth0.com/t/re-authenticate-the-user-with-a-new-organization-context/198993/4 "2026-04-21T15:49:48Z")

</div>

Hi @binh,

The question was tackled on another post, so for more information you can check out this topic - [Auto-provisioning users into correct organization based on email domain](https://community.auth0.com/t/auto-provisioning-users-into-correct-organization-based-on-email-domain/199018).

Thank you!  
Best regards,  
Remus

---

<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:** [May 5, 2026, 3:50pm UTC](https://community.auth0.com/t/re-authenticate-the-user-with-a-new-organization-context/198993/5 "2026-05-05T15:50:00Z")

</div>

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