> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Actions と複数のカスタムドメインの連携

> Auth0 Actions で複数のカスタムドメインを使用し、ドメインごとの認証ロジックを作成する方法を学びます。

[Auth0 Actions](/docs/docs/ja-jp/customize/actions) を使用すると、カスタムコードで認証をカスタマイズできます。[複数のカスタムドメイン](/docs/docs/ja-jp/customize/custom-domains/multiple-custom-domains) を使用している場合は、Actions でカスタムドメインの情報にアクセスして、ドメイン固有のロジックを実装したり、ユーザーを組織に振り分けたり、アクセスポリシーを適用したり、通知をカスタマイズしたりできます。

## カスタムドメイン情報へのアクセス

Actions では、`event.custom_domain` オブジェクトを通じてカスタムドメインの情報にアクセスできます。このオブジェクトには、ドメイン名と、そのドメインに設定されたメタデータが含まれます。

### 利用可能なトリガー

`event.custom_domain` オブジェクトは、以下の Action トリガーで利用できます。

| トリガー | イベントオブジェクト の参照 |
| - | - |
| Credentials Exchange | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/credentials-exchange/credentials-exchange-event-object) |
| カスタムメールプロバイダー | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/custom-email-provider/custom-email-provider-event-object) |
| カスタム電話プロバイダー | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/custom-phone-provider/custom-phone-provider-event-object) |
| Post Login | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/post-login/post-login-event-object) |
| Post User Registration | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/post-user-registration/post-user-registration-event-object) |
| Post Password Change | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/post-change-password/post-change-password-event-object) |
| Pre User Registration | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/pre-user-registration/pre-user-registration-event-object) |
| Reset Password Post Challenge | [イベントオブジェクト](/docs/docs/ja-jp/actions/reference/password-reset-post-challenge/password-reset-post-challenge-event-object) |

> [!NOTE]
>
> `event.custom_domain` オブジェクトは、リクエストがカスタムドメイン経由で行われたトリガーでのみ利用できます。カスタムドメインが使用されていない場合、このプロパティは `undefined` になります。

### イベントオブジェクトの構造

ドメインメタデータが設定された `login.example.com` ドメインを使用する場合:

| キー | 値 |
| - | - |
| allow\_list | example1.com,example2.com |

```javascript theme={null}
exports.onExecutePostLogin = async (event, api) => {
  console.log(event.custom_domain);
};
```

コンソールには次のように出力されます:

```json theme={null}
{
  "domain": "login.example.com",
  "domain_metadata": {
    "allow_list": "example1.com,example2.com"
  }
}
```

## よくあるユースケース

### ドメインごとの条件分岐

カスタムドメインと標準ドメインのどちらを使用しているかに応じて、異なるルールを適用します。

```javascript theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const domain = event.custom_domain?.domain;

  if (
    domain === undefined ||
    domain.includes('.auth0.com') || // パブリッククラウド
    domain.includes('.auth0app.com') // プライベートクラウド
  ) {
    return api.access.deny('Please use the custom domain to log in.');
  }
};
```

### メールドメインに基づくアクセス制御

カスタムドメインのメタデータを使用して、メールドメインごとのアクセスポリシーを適用します。

```javascript theme={null}
const getEmailDomain = (email) => {
  if (!email || typeof email !== 'string') {
    return null;
  }

  const parts = email.split('@');
  
  // '@' がちょうど1つあること（または少なくともドメイン部分が存在すること）を確認する
  // "invalid-email" や "user@" のようなケースに対応する
  if (parts.length < 2 || !parts[1]) {
    return null;
  }

  return parts[1].toLowerCase().trim();
}

exports.onExecutePreUserRegistration = async (event, api) => {
  const domain = event.custom_domain?.domain;

  if (domain === undefined) {
    return api.access.deny(
      'access_denied',
      `Access denied - Users cannot access without custom domain.`
    );
  }

  const email = event.user.email;
  
  if (email === undefined) {
    return api.access.deny(
      'access_denied',
      `Access denied - Users cannot access without email.`
    );
  }

  const domainAllowList = event.custom_domain?.domain_metadata?.allow_list?.split(',') || [];
  const emailDomain = getEmailDomain(email);

  if (domainAllowList.includes(emailDomain) === false) {
    return api.access.deny(
      'access_denied',
      `Access denied - Users from ${emailDomain} cannot access ${domain}.`
    );
  }
};
```

### アプリケーション メタデータに基づくアクセス制御

アプリケーションおよびドメイングループのアクセス ポリシーを、アプリケーションとドメインのメタデータに基づいて適用します:

```javascript theme={null}
exports.onExecuteCredentialsExchange = async (event, api) => {
  const domain = event.custom_domain?.domain;
  
  if (
    domain === undefined ||
    domain.includes(event.tenant.id)
  ) {
    return; // 正規ドメインはスキップ
  }

  const applicationGroup = new Set(event.client.metadata?.domain_group?.split(',') || []);
  const domainGroup = event.custom_domain?.domain_metadata?.domain_group?.split(',') || [];
  const intersection = domainGroup.filter(x => applicationGroup.has(x));

  if (intersection.length === 0) {
    return api.access.deny(
      'invalid_request',
      `Access denied - Cannot get access from application ${event.client.name} and ${domain}.`
    );
  }
};
```

### 接続メタデータに基づくアクセス制御

接続およびドメイングループのアクセスポリシーを、接続とドメインのメタデータを使用して適用します:

```javascript theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const domain = event.custom_domain?.domain;

  if (domain === undefined) {
    return api.access.deny(
      `Access denied - Users cannot access without custom domain.`
    );
  }

  const connectionGroup = new Set(event.connection.metadata?.domain_group?.split(',') || []);
  const domainGroup = event.custom_domain?.domain_metadata?.domain_group?.split(',') || [];
  const intersection = domainGroup.filter(x => connectionGroup.has(x));

  if (intersection.length === 0) {
    return api.access.deny(
      `Access denied - Users cannot access connection ${event.connection.name} from ${domain}.`
    );
  }
};
```

### 組織のメタデータに基づくアクセス制御

organization とドメイングループに対するアクセスポリシーを、organization とドメインのメタデータを使用して適用します：

```javascript theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const organization = event.organization;

  if (organization === undefined) {
    return; // organization以外の認証はスキップ
  }

  const domain = event.custom_domain?.domain;

  if (domain === undefined) {
    return api.access.deny(
      `Access denied - Users cannot access without custom domain.`
    );
  }

  const organizationGroup = new Set(organization.metadata?.domain_group?.split(',') || []);
  const domainGroup = event.custom_domain?.domain_metadata?.domain_group?.split(',') || [];  
  const intersection = domainGroup.filter(x => organizationGroup.has(x));

  if (intersection.length === 0) {
    return api.access.deny(
      `Access denied - Users cannot access organization ${organization.name} from ${domain}.`
    );
  }
};
```

### リージョン別のリクエスト

カスタムドメインのメタデータに基づいて、リージョンごとの外部サービスにリクエストを送信します。

```javascript theme={null}
exports.onExecuteCustomEmailProvider = async (event, api) => {
  const regionServiceEndpoint = event.custom_domain?.domain_metadata?.region_service_endpoint;

  if (regionServiceEndpoint === undefined) {
    return api.notification.drop(`Missing regional service endpoint configuration at custom domain.`);
  }

  const notification = event.notification;
  const messageBody = {
    body: notification.html
  };

  try {
    await fetch(regionServiceEndpoint, {
      method: 'POST',
      headers: {
        'X-API-Key': event.secrets.API_KEY,
      },
      body: JSON.stringify(messageBody),
    });
  } catch (err) {
    api.notification.drop('External service failure');
  }
};
```

## 詳しく見る

### Actions のドキュメント

* [Auth0 Actions の概要](/docs/docs/ja-jp/customize/actions)
* [Action トリガーを確認する](/docs/docs/ja-jp/customize/actions/explore-triggers)
* [Actions の制限事項](/docs/docs/ja-jp/customize/actions/limitations)

### イベントオブジェクトのリファレンス

* [Post-Login イベントオブジェクト](/docs/docs/ja-jp/actions/reference/post-login/post-login-event-object)
* [Pre-User Registration イベントオブジェクト](/docs/docs/ja-jp/actions/reference/pre-user-registration/pre-user-registration-event-object)
* [カスタムメールプロバイダー イベントオブジェクト](/docs/docs/ja-jp/actions/reference/custom-email-provider/custom-email-provider-event-object)
* [カスタム電話プロバイダー イベントオブジェクト](/docs/docs/ja-jp/actions/reference/custom-phone-provider/custom-phone-provider-event-object)

### 関連情報

* [複数のカスタムドメイン](/docs/docs/ja-jp/customize/custom-domains/multiple-custom-domains)
* [Organizations](/docs/docs/ja-jp/manage-users/organizations)
* [カスタムメールプロバイダー](/docs/docs/ja-jp/customize/email/smtp-email-providers/custom)
