> ## 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.

> ログイン時の Action と post-login Action の間でユーザーまたはアプリケーションのメタデータを受け渡す方法について説明します。

# Actions トランザクションメタデータ

Actions トランザクションメタデータでは、トランザクションの間、[post-login](/ja/docs/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger) Action 内でカスタムメタデータを保存、参照、共有できます。

以前は各 Action がそれぞれ独立して動作していたため、それらの間で情報を受け渡すのは困難でした。Actions トランザクションメタデータを使用すると、次のことが可能になります。

* API レスポンスや中間的な計算結果などのデータを Actions 間で共有する。
* 異なる Actions で同じ情報を再取得または再計算する必要をなくす。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  開発でトランザクションメタデータを使用する前に、制限事項を確認することをお勧めします。詳しくは、[Actions の制限事項](/ja/docs/customize/actions/limitations)を参照してください。
</Callout>

<div id="how-it-works">
  ## 仕組み
</div>

トランザクションメタデータを保存するためのキーと値のペアを設定するには、[post-login API object](/ja/docs/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger/post-login-api-object) `api.transaction.setMetadata` を使用します。

保存したキーと値のペアにアクセスするには、[post-login Event object](/ja/docs/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger/post-login-event-object) `event.transaction.metadata` を使用します。これにより、1 回の実行中に、同じ Action または post-login トリガーの後続の Actions からアクセスできます。

API オブジェクトと Event オブジェクトは、次のパラメーターを受け取ります。

| 値       | 型                             | 説明                                                  |
| ------- | ----------------------------- | --------------------------------------------------- |
| `Key`   | `String`                      | 設定するメタデータ プロパティのキーです。                               |
| `Value` | `String`, `Number`, `Boolean` | メタデータ プロパティの値です。<br />`null` に設定すると、そのプロパティは削除されます。 |

Actions の記述方法の詳細については、[Write Your First Action](/ja/docs/customize/actions/write-your-first-action) を参照してください。

<div id="latency">
  ## レイテンシー
</div>

Actions Transaction Metadata を使用すると、わずかな追加レイテンシーが発生する可能性があります。レイテンシーはメタデータのペイロードサイズに比例し、Action の中断が発生する場合に特に影響します。たとえば、<Tooltip tip="" cta="用語集を見る" href="/ja/docs/glossary?term=MFA">MFA</Tooltip> をトリガーする場合、Actions からリダイレクトする場合、または Forms をレンダリングする場合は、ストレージからデータを再読み込みする必要があるため、レイテンシーの問題が発生する可能性があります。

それでも、この潜在的なレイテンシーは、一連の Actions が必要とするデータを取得するために冗長な外向き HTTP リクエストを行う場合に比べれば、最小限に抑えられるはずです。

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

<div id="access-metadata-immediately">
  ### メタデータにすぐにアクセスする
</div>

トランザクションメタデータにキーと値のペアを設定し、その値にすぐにアクセスできます。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('hello', 'Auth0');

  console.log('Hello ', event.transaction?.metadata?.hello);
  /* "Hello Auth0" を出力 */
};
```

<div id="set-supported-values">
  ### サポートされている値を設定する
</div>

`string`、`number`、`boolean` 型の値を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('string_value', 'Auth0');
  api.transaction.setMetadata('boolean_value', true);
  api.transaction.setMetadata('number_value', 12);

  console.log('string_value', event.transaction?.metadata?.string_value);
  /* "string_value Auth0" を出力 */
  console.log('boolean_value', event.transaction?.metadata?.boolean_value);
  /* "boolean_value true" を出力 */
  console.log('number_value', event.transaction?.metadata?.number_value);
  /* "number_value 12" を出力 */
};
```

<div id="serialize-values">
  ### 値をシリアライズする
</div>

値は、[制限事項](/ja/docs/customize/actions/limitations#transaction-metadata)の範囲内で`strings`としてシリアライズします。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  `object`型または`array`型の値は設定できませんが、`strings`としてシリアライズすることはできます。
</Callout>

```text lines expandable theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const serialized_object_value = JSON.stringify({
    string_value: 'Auth0',
    boolean_value: true,
    number_value: 12
  });

  api.transaction.setMetadata('serialized_object_value', serialized_object_value);

  const serialized_array_value = JSON.stringify([
    'Auth0',
    true,
    12
  ]);
  api.transaction.setMetadata('serialized_array_value', serialized_array_value);

  console.log('serialized_object_value',
    JSON.parse(event.transaction?.metadata?.serialized_object_value)
  );
  /* 出力: "serialized_object_value { string_value: 'Auth0', boolean_value: true, number_value: 12 }" */

  console.log('serialized_array_value',
    JSON.parse(event.transaction?.metadata?.serialized_array_value)
  );
  /* 出力: "serialized_array_value [ 'Auth0', true, 12 ]" */
};
```

<div id="share-values-between-actions">
  ### Actions 間で値を共有する
</div>

同じ実行シーケンス内の Actions 間でキーと値のペアを共有します。

**Action 1**

`api` オブジェクトの `setMetadata` メソッドを使用して、`hello` キーに `Auth0` という値を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('hello', 'Auth0');
};
```

**Action 2**

設定した値にアクセスするには、`event` オブジェクトの `transaction.metadata` プロパティを使用して、トランザクションメタデータの `hello` キーに設定された `Auth0` の値をログに出力します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  console.log('Hello', event.transaction?.metadata?.hello);
  /* "Hello Auth0" を出力 */
};
```

<div id="update-metadata">
  ### メタデータを更新する
</div>

既存のキーに別の値を設定して、メタデータを更新します。

**Action 1**

トランザクションメタデータのキーと値のペアとして `custom_tx_id` と `xyz123` を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
```

**Action 2**

キーを `custom_tx_id`、値を `xyz123` としてログに記録します。次に、`custom_tx_id` を `abc456` に設定し、トランザクションメタデータ内の `custom_tx_id` の最新の値でもう一度ログに記録します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id xyz123" を出力 */

  api.transaction.setMetadata('custom_tx_id', 'abc456');

  console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id abc456" を出力 */
};
```

**Action 3**

`custom_tx_id` の値 `abc456` をログに記録します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id abc456" を出力 */
};
```

<div id="remove-metadata">
  ### メタデータを削除する
</div>

各キーの値を `null` に設定して、トランザクションメタデータの値を削除します。

**Action 1**

トランザクションメタデータに `custom_tx_id` を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
```

**Action 2**

`custom_tx_id` を `null` に設定し、`custom_tx_id` の値として `null` をログに記録します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id xyz123" を出力 */

  api.transaction.setMetadata('custom_tx_id', null);

  console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id undefined" を出力 */
};
```

**Action 3**

`custom_tx_id` の値として `null` をログ出力します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id undefined" を出力 */
};
```

<div id="preserve-values-on-redirect-to-external-sites">
  ### 外部サイトへのリダイレクト時に値を保持する
</div>

[redirects](/ja/docs/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger/redirect-with-actions) の間も、トランザクションメタデータは保持されます。値は、ユーザーが認証フローを続行した際に使用できます。

**Action 1**

トランザクションメタデータに `custom_tx_id` を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
```

**Action 2**

トランザクションのメタデータにある `custom_tx_id` を含むトークンを送信して、外部サイトにリダイレクトします。続いて、トランザクションのメタデータ内の `custom_tx_id` の値を、外部サイトに渡し、その後別のトークンのペイロードで返送された値と比較します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  const token = api.redirect.encodeToken({
    secret: event.secrets.REDIRECT_SECRET,
    expiresInSeconds: 60, 
    payload: {
      custom_tx_id: event.transaction?.metadata?.custom_tx_id,
      continue_uri: `https://${event.secrets.TENANT_DOMAIN}/continue`
    },
  });

  api.redirect.sendUserTo(event.secrets.REDIRECT_URL, {
    query: { session_token: token }
  });
};

exports.onContinuePostLogin = async (event, api) => {
  const payload = api.redirect.validateToken({
    secret: event.secrets.REDIRECT_SECRET
  });

  console.log('Does custon_tx_id match?',
    payload?.custom_tx_id === event.transaction?.metadata?.custom_tx_id
  );
  /* 出力: "Does custon_tx_id match? True" */
};
```

<div id="preserve-values-on-forms-rendering">
  ### Forms のレンダリング時に値を保持する
</div>

[Actions を使用して Forms をレンダリングする](/ja/docs/customize/forms/render) 場合、ユーザーが認証フローを続行しても、トランザクションメタデータの値は保持され、引き続き利用できます。

Forms で Actions を使用する方法の詳細については、[Actions を使用して Forms をレンダリングする](/ja/docs/customize/forms/render) を参照してください。

**Action 1**

トランザクションメタデータに `custom_tx_id` を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
```

**Action 2**

Form をレンダリングします。続いて、Actions の実行を再開する際に、保持された `custom_tx_id` をログに出力します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.prompt.render(event.secrets.FORM_ID);
};

exports.onContinuePostLogin = async (event, api) => {
  console.log('custon_tx_id', event.transaction?.metadata?.custom_tx_id);
  /* "custom_tx_id xyz123" を出力 */
};
```

<div id="share-values-with-forms">
  ### Forms と値を共有する
</div>

Actions を使用して Forms をレンダリングし、トランザクションメタデータの値を Forms に渡すことができます。

**Action 1**

トランザクションメタデータに `custom_tx_id` を設定します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
```

**Action 2**

トランザクションメタデータ内の `custom_tx_id` を `vars` パラメーターとして渡して Form をレンダリングします。続いて、Actions の実行を再開する際に、トランザクションメタデータ内の `custom_tx_id` の値と Form に渡した値を比較します。

```text lines theme={null}
exports.onExecutePostLogin = async (event, api) => {
  api.prompt.render(event.secrets.FORM_ID, {
    vars: {
      custom_tx_id: event.transaction?.metadata?.custom_tx_id
    }
  });
};

exports.onContinuePostLogin = async (event, api) => {
  console.log('Does custon_tx_id match?',
    event.prompt?.vars?.custom_tx_id === event.transaction?.metadata?.custom_tx_id
  );
  /* 出力: "Does custon_tx_id match? True" */
};
```
