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

# メール変更スクリプトのテンプレート

> ユーザーのメールアドレスを変更するカスタムデータベースアクションスクリプトについて説明します。

メール変更スクリプトは、ユーザーのメールアドレスやその検証ステータスが変わった際に、定義済みの関数を実行します。この関数の名前を`changeEmail`にすることをお勧めします。

このスクリプトは[レガシー認証シナリオ](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/overview-custom-db-connections) でのみ使用され、ユーザーのメールアドレス（もしくはメールアドレス検証ステータス）をAuth0と外部データベースで同時に更新したい場合には必須です。

メール変更スクリプトは、<Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-JP/ja-jp/glossary?term=auth0-dashboard" tip="Auth0 Dashboard: サービスを構成するためのAuth0の主製品。" cta="用語集の表示">Auth0 Dashboard</Tooltip>では構成できません。このスクリプトを管理するには、Auth0 <Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-JP/ja-jp/glossary?term=management-api" tip="Management API: 顧客が管理タスクを実行できるようにするための製品。" cta="用語集の表示">Management API</Tooltip>の[接続の作成](https://auth0.com/docs/api/management/v2#!/Connections/post_connections)または[接続の更新](https://auth0.com/docs/api/management/v2#!/Connections/patch_connections_by_id)エンドポイント、または[Auth0 Deploy CLI](/docs/ja-JP/ja-jp/deploy-monitor/deploy-cli-tool)を使用する必要があります。

<Warning>
  Auth0 Dashboardを使ってカスタムデータベース接続のデータベースアクションスクリプトを管理している場合には、保存時にメール変更スクリプトが自動的に削除されます。

  メール変更スクリプトが誤って削除されていないことを確認するには、Auth0 Management APIまたはAuth0 Deploy CLIを使って接続を管理してください。

  カスタムデータベースでユーザーのメールを手動で変更する場合には、ユーザーに個別に適用しなければなりません。Auth0は変更を自動的には検知しません。
</Warning>

<div id="changeemail-function">
  ## ChangeEmail関数
</div>

changeEmail関数は以下を行います。

* 外部データベースでユーザーのメールアドレスを更新します。
* 処理の失敗やエラーが発生した場合にはエラーを返します。

<div id="definition">
  ### 定義
</div>

`changeEmail`関数は、4つのパラメーターを受け取り、`コールバック`関数を返します。

```js lines theme={null}
changeEmail(email, newEmail, verified, callback): function
```

<table class="table">
  <thead>
    <tr>
      <th><b>パラメーター</b></th>
      <th><b>タイプ</b></th>
      <th><b>説明</b></th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`email`</td>
      <td>文字列</td>
      <td>ユーザーの現在のメールアドレス。</td>
    </tr>

    <tr>
      <td>`newEmail`</td>
      <td>文字列</td>
      <td>外部データベースに設定するユーザーの新しいメールアドレスの値。</td>
    </tr>

    <tr>
      <td>`verified`</td>
      <td>ブール値</td>
      <td>新しいメールアドレスの確認状況。</td>
    </tr>

    <tr>
      <td>`callback`</td>
      <td>関数</td>
      <td>エラーデータをパイプライン経由で渡すために使用。</td>
    </tr>
  </tbody>
</table>

<div id="example">
  ### 例
</div>

これは疑似JavaScriptを使った例で、どのようにすれば`changeEmail`関数を実装できるかがわかります。

```javascript lines expandable theme={null}
function (email, newEmail, verified, callback) {
  // Prepare the API call
  let options = {
    url: "https://example.com/api/users",
    action: "PATCH",
    body: {
      email: email,
      new_email: newEmail,
      email_verified: verified
    }
  };

  // Call the API
  send(options, err => {
    // Return `false` value in callback if operation failed
    if (err && err.id == "FAIL_CHANGE_EMAIL") {
      return callback(null, false);
    } else if (err) {
      // Return error in callback if unspecified error occurred
      return callback(new Error("My custom error message."));
    }

    // Return `true` value in callback if operation succeeded
    return callback(null, true);
  });
}
```

<div id="callback-function">
  ## コールバック関数
</div>

`callback`関数は2つのパラメーターを受け取り、1つの関数を返します。

<div id="definition">
  ### 定義
</div>

```js lines theme={null}
callback(error, operationResult): function
```

<table class="table">
  <thead>
    <tr>
      <th><b>パラメーター</b></th>
      <th><b>タイプ</b></th>
      <th><b>必須</b></th>
      <th><b>説明</b></th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`error`</td>
      <td>オブジェクト</td>
      <td>必須</td>
      <td>エラーデータを含む。</td>
    </tr>

    <tr>
      <td>`operationResult`</td>
      <td>ブール値</td>
      <td>任意</td>
      <td>メール変更操作の結果を示す。</td>
    </tr>
  </tbody>
</table>

<div id="return-a-success">
  ### 成功の場合
</div>

メールアドレス変更操作が成功した場合、`callback`関数を返し、`error`パラメーターには`null`を、`operationResult`パラメーターには`true`を渡します。

```js lines theme={null}
return callback(null, true);
```

<div id="return-a-failure">
  ### 失敗の場合
</div>

メールアドレス変更操作が失敗した場合、`callback`関数を返し、`error`パラメーターには`null`を、`operationResult`パラメーターには`false`を渡します。

```js lines theme={null}
return callback(null, false);
```

<div id="return-an-error">
  ### エラーの場合
</div>

エラーが発生した場合は、`callback`関数を返し、`error`パラメーターに関連するエラー情報を渡します。

```js lines theme={null}
return callback(new Error("My custom error message."));
```

<div id="learn-more">
  ## もっと詳しく
</div>

* [パスワード変更スクリプトのテンプレート](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/templates/change-password)
* [作成スクリプトのテンプレート](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/templates/create)
* [スクリプトのテンプレートを削除する](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/templates/delete)
* [ユーザー取得スクリプトのテンプレート](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/templates/get-user)
* [ログインスクリプトのテンプレート](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/templates/login)
* [検証スクリプトのテンプレート](/docs/ja-JP/ja-jp/authenticate/database-connections/custom-db/templates/verify)
