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

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

> ログインおよびログイン後Actions間で、ユーザーまたはアプリケーションメタデータを渡す方法について説明します。

Actionsトランザクションメタデータは、トランザクションの期間中、[ログイン後](/docs/ja-jp/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger)Action内でカスタムメタデータを保存、アクセスおよび/または共有します。

以前は、各Actionが個別に機能していたため、Action間で情報を渡すことは困難でした。Actionトランザクションメタデータを使用すると、以下が可能になります。

* API応答や中間計算など、Actions全体でデータを共有する。
* 異なるActionで同じ情報を再取得または再計算する必要をなくす。

<Note>
  トランザクションメタデータを開発で使用する前に、制限事項の確認をお勧めします。詳しくは、「[Actionsの制限事項](/docs/ja-jp/customize/actions/limitations)」をお読みください。
</Note>

## 仕組み

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

[ログイン後イベントオブジェクト](/docs/ja-jp/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger/post-login-event-object) `event.transaction.metadata`を使用して、ログイン後トリガーで同じActionまたは後続Actions内に保存されたキー/値のペアにアクセスし、1回実行します。

APIとイベントオブジェクトは、以下のパラメーターを受け入れます。

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

Actionsの作成について詳しくは、「[初めてのActionを作成する](/docs/ja-jp/customize/actions/write-your-first-action)」をお読みください。

## レイテンシー

Actionsトランザクションメタデータを使用すると、名目上の追加のレイテンシーが発生する可能性があります。すべてのレイテンシーはメタデータのペイロードサイズに比例し、Action停止が発生した場合、重要になります。例えば、<Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-jp/glossary?term=multifactor-authentication" tip="多要素認証(MFA): ユーザー名とパスワードに加えて、SMS経由のコードなどの要素を使用するユーザー認証プロセス。" cta="用語集の表示">MFA</Tooltip>のトリガー、Actionsからのリダイレクト、Formsの表示などは、ストレージからデータを再読み込みする必要があるため、レイテンシーの問題が発生する可能性があります。

ただし、潜在的なレイテンシーは、Actionsのシーケンスが必要とするデータを取得するための冗長的な発信HTTPリクエストより最小にするべきです。

## 例

### メタデータに即座にアクセスする

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

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

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

### Set supported values

Set values of types: `string`, `number`, and `boolean`.

```javascript 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);
  /* Outputs "string_value Auth0" */
  console.log('boolean_value', event.transaction?.metadata?.boolean_value);
  /* Outputs "boolean_value true" */
  console.log('number_value', event.transaction?.metadata?.number_value);
  /* Outputs "number_value 12" */
};
```

### 値をシリアル化する

値を[制限](https://auth0.com/docs/customize/actions/limitations#transaction-metadata)内で`strings`としてシリアル化します。

<Note>
  タイプの値`object`または`array`は設定できませんが、`strings`としてシリアル化できます。
</Note>

```javascript lines 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)
  );
  /* Outputs "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)
  );
  /* Outputs "serialized_array_value [ 'Auth0', true, 12 ]" */
};
```

### Actions間で値を共有する

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

**Action 1**

`Auth0`値のある`hello`キーを`api`オブジェクト`setMetadata`方法で設定します。

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

**Action 2**

トランザクションメタデータで`hello`キーに`Auth0`値を`event`オブジェクト`transaction.metadata`プロパティでログ記録し、設定値にアクセスします。

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

### メタデータを更新する

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

**Action 1**

トランザクションメタデータキー/値を`custom_tx_id`および`xyz123`に設定します。

```javascript 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`の最新の値をもう一度ログ記録します。

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

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

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

**Action 3**

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

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

### メタデータを削除する

特定の各キーの値を無効にすることで、トランザクションメタデータ値を削除します。

**Action 1**

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

```javascript 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`値をログ記録します。

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

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

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

**Action 3**

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

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

### 外部サイトへのリダイレクトで値を保持する

[リダイレクト](/docs/ja-jp/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger/redirect-with-actions)中にトランザクションメタデータを保持します。値は、ユーザーが認証フローを継続する場合に利用できます。

**Action 1**

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

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

**Action 2**

トランザクションメタデータから`custom_tx_id`のあるトークンを送信して外部サイトにリダイレクトします。次に、トランザクションメタデータで、外部サイトに渡され、別のトークンのペイロードで送り戻された値と`custom_tx_id`値を比較します。

```javascript 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
  );
  /* Outputs "Does custon_tx_id match? True" */
};
```

### Forms表示で値を保持する

[Actionsを使用してFormsを表示](/docs/ja-jp/customize/forms/render)すると、トランザクションメタデータ値は保持され、ユーザーが認証フローを継続する場合に使用できます。

Actionsを使用したFormsの使用について詳しくは、「[Actionsを使用したFormsの表示](/docs/ja-jp/customize/forms/render)」をお読みください。

**Action 1**

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

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

**Action 2**

Formを表示します。その後、Actions実行を継続する場合は、保持された`custom_tx_id`がログ記録されます。

```javascript 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);
  /* Outputs "custom_tx_id xyz123" */
};
```

### Formsと値を共有する

Actionsを使用してFormsを表示し、トランザクションメタデータ値をFormに渡すことができます。

**Action 1**

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

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

**Action 2**

`custom_tx_id`をトランザクションメタデータから`vars`パラメーターとして渡し、Formを表示します。その後、Actions実行を継続する場合は、トランザクションメタデータで、Formに渡された値と`custom_tx_id`値を比較します。

```javascript 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
  );
  /* Outputs "Does custon_tx_id match? True" */
};
```
