How to Change Organization Metadata in Actions

Overview

This article explains how to change the organization metadata when registering the first user. It will also explain why a post-login trigger should be used, not a post-registration trigger.

Applies To

  • Organizations
  • Metadata
  • Actions

Solution

For this, a post-login trigger should be used because the event object contains the organization’s details, and the post-registration trigger cannot be used for social or enterprise connections.

Update an organization’s metadata through the Management API, specifically by calling the Modify an Organization endpoint.

Here is an example of adding a custom attribute in the organization metadata:

NOTE: This is a proof-of-concept example and is not yet production-ready. This should be tested in a lab environment before implementing.

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

  const management = new ManagementClient({
    clientId: event.secrets.client_id,
    clientSecret: event.secrets.client_secret,
    domain: event.secrets.domain,
    timeout: 3000 // Timeout in milliseconds
  });

  try {
    const result = await management.organizations.update(
      { id: event.organization?.id }, 
      {
        metadata: {
          custom_attribute: 'value'
        }
      }
    );
    console.log('Organization metadata updated successfully:', result.data.metadata);
  } catch (error) {
    console.error('Error updating organization metadata:', error);
  }