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

> Delegated Administration Extension でカスタムドメイン Hook を使用する方法を説明します。

# Delegated Administration: カスタムドメイン Hook

Delegated Administration Extension で [複数のカスタムドメイン](/docs/ja-jp/customize/custom-domains/multiple-custom-domains) を使用する場合、**カスタムドメイン Hook** を使うと、ユーザーの作成、パスワードのリセット、メールアドレスの確認、ユーザーのブロックといった操作に使用するカスタムドメインを指定できます。

<div id="hook-contract">
  ## Hook の仕様
</div>

* **ctx**: 現在のリクエストに関する情報を含むコンテキストオブジェクト。
  * **method**: 実行中の操作 (例: `create`、`reset_password`、`verify_email`、`block_user`) 。
  * **payload**: 操作に関連するデータ (`create` メソッドでは、メールアドレスなどのユーザー詳細を含みます) 。
  * **request**: リクエストに関する情報を含みます。
    * **originalUser**: 操作対象のユーザー (`create` 以外のメソッドの場合) 。
* **callback(error, result)**: カスタムドメインの設定を返すためのコールバック関数。
  * **error**: 問題が発生した場合はエラーオブジェクト、それ以外の場合は `null`。
  * **result**: カスタムドメインの動作を指定するオブジェクト:
    * **customDomain** (string): 使用するカスタムドメイン (例: `'customone.example.com'`) 。
    * **useCanonicalDomain** (boolean): カスタムドメインではなく テナント の正規ドメインを使用する場合は、`true` に設定します。
    * テナント の[デフォルトドメイン](/docs/ja-jp/customize/custom-domains/multiple-custom-domains)に委譲するには、空のオブジェクト `{}` を返します。

<div id="supported-methods">
  ## サポート対象のメソッド
</div>

カスタムドメイン Hook は、次のユーザー操作で呼び出されます。

* **create**: 新しいユーザーを作成するとき
* **reset\_password**: パスワードリセットメールを送信するとき
* **verify\_email**: メールアドレス確認用の確認リンクを送信するとき
* **block\_user**: ユーザーをブロックするとき

<div id="sample-use">
  ## 使用例
</div>

この例では、メールドメインに応じてユーザーが異なるカスタムドメインに振り分けられます。企業ドメインのユーザーは canonical テナント domain を使用し、それ以外のすべてのユーザーはテナントのデフォルトドメインを使用します。

```javascript lines theme={null}
function(ctx, callback) {
  // メールドメインをカスタムドメインにマッピング
  const emailToDomain = {
    'customone.com': 'customone.example.com',
    'customtwo.com': 'customtwo.example.com',
  };

  // メールからカスタムドメインを取得するヘルパー関数
  function getDomainFromEmail(userEmail) {
    if (!userEmail) return null;
    const domain = userEmail.split('@')[1];
    return emailToDomain[domain];
  }

  // 正規ドメインを使用するか判定するヘルパー関数
  function shouldUseCanonical(userEmail) {
    if (!userEmail) return false;
    const domain = userEmail.split('@')[1];
    return domain === 'corp.com';
  }

  // 操作の種類に応じてユーザーのメールアドレスを取得
  let userEmail;
  if (ctx.method === 'create') {
    // 作成操作の場合、メールアドレスはペイロードに含まれる
    userEmail = ctx.payload.email;
  } else {
    // その他の操作の場合、元のユーザー情報からメールアドレスを取得
    userEmail = ctx.request.originalUser.email;
  }

  const customDomain = getDomainFromEmail(userEmail);

  // オプション1: 特定のカスタムドメインを使用
  if (customDomain) {
    return callback(null, {
      customDomain: customDomain
    });
  }

  // オプション2: 正規テナントドメインを使用
  if (shouldUseCanonical(userEmail)) {
    return callback(null, {
      useCanonicalDomain: true
    });
  }

  // オプション3: テナントのデフォルトドメインを使用（設定されている場合）
  // 空のオブジェクトを返してデフォルトドメインに委譲する
  return callback(null, {});
}
```

<div id="notes">
  ## 注記
</div>

* このフックが設定されていない場合、Delegated Administration Extension は、テナントに設定されているデフォルトのカスタムドメインがあればそれを使用し、ない場合は正規ドメインを使用します。
* 返されるカスタムドメインは、テナントに設定された検証済みのカスタムドメインである必要があります。無効なドメインまたは未検証のドメインを指定すると、操作は失敗します。
* `useCanonicalDomain: true` を使用すると、メールとリンクでは Auth0 テナントの正規ドメイン (例: `YOUR_TENANT.auth0.com` または `YOUR_TENANT.REGION.auth0.com`) が使用されます。
* 空のオブジェクト `{}` を返すと、ドメインの選択はテナントの[デフォルトのカスタムドメイン](/docs/ja-jp/customize/custom-domains/multiple-custom-domains)設定に委ねられます。

<div id="learn-more">
  ## 詳細はこちら
</div>

* [複数のカスタムドメイン](/docs/ja-jp/customize/custom-domains/multiple-custom-domains)
* [Delegated Administration: Access Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-access-hook)
* [Delegated Administration: Filter Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook)
* [Delegated Administration: Write Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-write-hook)
* [Delegated Administration: 設定クエリ Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-settings-query-hook)
