Actions トランザクションメタデータでは、トランザクションの間、post-login Action 内でカスタムメタデータを保存、参照、共有できます。
以前は各 Action がそれぞれ独立して動作していたため、それらの間で情報を受け渡すのは困難でした。Actions トランザクションメタデータを使用すると、次のことが可能になります。
- API レスポンスや中間的な計算結果などのデータを Actions 間で共有する。
- 異なる Actions で同じ情報を再取得または再計算する必要をなくす。
開発でトランザクションメタデータを使用する前に、制限事項を確認することをお勧めします。詳しくは、Actions の制限事項を参照してください。
トランザクションメタデータを保存するためのキーと値のペアを設定するには、post-login API object api.transaction.setMetadata を使用します。
保存したキーと値のペアにアクセスするには、post-login Event object event.transaction.metadata を使用します。これにより、1 回の実行中に、同じ Action または post-login トリガーの後続の Actions からアクセスできます。
API オブジェクトと Event オブジェクトは、次のパラメーターを受け取ります。
| 値 | 型 | 説明 |
|---|
Key | String | 設定するメタデータ プロパティのキーです。 |
Value | String, Number, Boolean | メタデータ プロパティの値です。
null に設定すると、そのプロパティは削除されます。 |
Actions の記述方法の詳細については、Write Your First Action を参照してください。
Actions Transaction Metadata を使用すると、わずかな追加レイテンシーが発生する可能性があります。レイテンシーはメタデータのペイロードサイズに比例し、Action の中断が発生する場合に特に影響します。たとえば、 をトリガーする場合、Actions からリダイレクトする場合、または Forms をレンダリングする場合は、ストレージからデータを再読み込みする必要があるため、レイテンシーの問題が発生する可能性があります。
それでも、この潜在的なレイテンシーは、一連の Actions が必要とするデータを取得するために冗長な外向き HTTP リクエストを行う場合に比べれば、最小限に抑えられるはずです。
トランザクションメタデータにキーと値のペアを設定し、その値にすぐにアクセスできます。
exports.onExecutePostLogin = async (event, api) => {
api.transaction.setMetadata('hello', 'Auth0');
console.log('Hello ', event.transaction?.metadata?.hello);
/* "Hello Auth0" を出力 */
};
string、number、boolean 型の値を設定します。
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" を出力 */
};
値は、制限事項の範囲内でstringsとしてシリアライズします。
object型またはarray型の値は設定できませんが、stringsとしてシリアライズすることはできます。
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 ]" */
};
同じ実行シーケンス内の Actions 間でキーと値のペアを共有します。
Action 1
api オブジェクトの setMetadata メソッドを使用して、hello キーに Auth0 という値を設定します。
exports.onExecutePostLogin = async (event, api) => {
api.transaction.setMetadata('hello', 'Auth0');
};
Action 2
設定した値にアクセスするには、event オブジェクトの transaction.metadata プロパティを使用して、トランザクションメタデータの hello キーに設定された Auth0 の値をログに出力します。
exports.onExecutePostLogin = async (event, api) => {
console.log('Hello', event.transaction?.metadata?.hello);
/* "Hello Auth0" を出力 */
};
既存のキーに別の値を設定して、メタデータを更新します。
Action 1
トランザクションメタデータのキーと値のペアとして custom_tx_id と xyz123 を設定します。
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 の最新の値でもう一度ログに記録します。
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 をログに記録します。
exports.onExecutePostLogin = async (event, api) => {
console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
/* "custom_tx_id abc456" を出力 */
};
各キーの値を null に設定して、トランザクションメタデータの値を削除します。
Action 1
トランザクションメタデータに custom_tx_id を設定します。
exports.onExecutePostLogin = async (event, api) => {
api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
Action 2
custom_tx_id を null に設定し、custom_tx_id の値として 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 をログ出力します。
exports.onExecutePostLogin = async (event, api) => {
console.log('custom_tx_id', event.transaction?.metadata?.custom_tx_id);
/* "custom_tx_id undefined" を出力 */
};
redirects の間も、トランザクションメタデータは保持されます。値は、ユーザーが認証フローを続行した際に使用できます。
Action 1
トランザクションメタデータに custom_tx_id を設定します。
exports.onExecutePostLogin = async (event, api) => {
api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
Action 2
トランザクションのメタデータにある custom_tx_id を含むトークンを送信して、外部サイトにリダイレクトします。続いて、トランザクションのメタデータ内の custom_tx_id の値を、外部サイトに渡し、その後別のトークンのペイロードで返送された値と比較します。
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" */
};
Actions を使用して Forms をレンダリングする 場合、ユーザーが認証フローを続行しても、トランザクションメタデータの値は保持され、引き続き利用できます。
Forms で Actions を使用する方法の詳細については、Actions を使用して Forms をレンダリングする を参照してください。
Action 1
トランザクションメタデータに custom_tx_id を設定します。
exports.onExecutePostLogin = async (event, api) => {
api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
Action 2
Form をレンダリングします。続いて、Actions の実行を再開する際に、保持された custom_tx_id をログに出力します。
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" を出力 */
};
Actions を使用して Forms をレンダリングし、トランザクションメタデータの値を Forms に渡すことができます。
Action 1
トランザクションメタデータに custom_tx_id を設定します。
exports.onExecutePostLogin = async (event, api) => {
api.transaction.setMetadata('custom_tx_id', 'xyz123');
};
Action 2
トランザクションメタデータ内の custom_tx_id を vars パラメーターとして渡して Form をレンダリングします。続いて、Actions の実行を再開する際に、トランザクションメタデータ内の custom_tx_id の値と Form に渡した値を比較します。
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" */
};