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

# Pre User Registration トリガーについて

> Pre User Registration アクション は、ユーザーが Database または パスワードレス 接続に追加される前に実行されます。ユーザープロファイルの作成前にメタデータを追加したり、登録を拒否したりできます。

`pre-user-registration` トリガーは、ユーザーが作成される前に、Database または パスワードレス (Passwordless: 最初の認証要素としてパスワードを使用しない認証方式です。) 接続を通じて登録しようとすると実行されます。このトリガーを使用して、ユーザープロファイルの作成前にメタデータを追加したり、カスタムロジックで登録を拒否したりできます。

![アクション の Pre User Registration フローを示す図。](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/cdy7uua7fh8z/2KtUcZhaBcT12GxjhBJZFG/9633b4454ac1c06c0deed6c97e70fe7d/pre-user-registration-flow.png)

このフローの アクション はブロッキング (同期) であり、トリガーの処理の一部として実行されます。アクション が完了するまで、Auth0 pipeline の後続の処理は実行されません。

> [!NOTE]
>
> 現在、`pre-user-registration` アクション を使用して、パスワードレスユーザーにメタデータを追加することはできません。

## 参照

* [イベントオブジェクト](/docs/docs/ja-jp/customize/actions/reference/pre-user-registration/pre-user-registration-event-object): 新規ユーザー登録リクエストに関するコンテキスト情報を提供します。
* [APIオブジェクト](/docs/docs/ja-jp/customize/actions/reference/pre-user-registration/pre-user-registration-api-object): フローの動作を変更するためのメソッドを提供します。

## 一般的なユースケース

### 場所に基づいて登録を拒否する

pre-user-registration Action を使用すると、ユーザーのサインアップを防止できます。

```javascript lines theme={null}
/**
 * @param {Event} event - 登録しようとしているユーザーとコンテキストに関する詳細。
 * @param {PreUserRegistrationAPI} api - サインアップの動作を変更するためのメソッドを提供するインターフェース。
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  if (event.request.geoip.continentCode === "NA") {

    // エラーメッセージをローカライズする 
    const LOCALIZED_MESSAGES = {
      en: 'You are not allowed to register.',
      es: 'No tienes permitido registrarte.'
    };

    const userMessage = LOCALIZED_MESSAGES[event.request.language] || LOCALIZED_MESSAGES['en'];
    api.access.deny('no_signups_from_north_america', userMessage);
  }
};
```

### ユーザープロファイルにメタデータを設定する

pre-user-registration Action を使用すると、ユーザープロファイルの作成前にメタデータを追加できます。

```javascript lines theme={null}
/**
 * @param {Event} event - 登録を試みているユーザーとコンテキストに関する詳細。
 * @param {PreUserRegistrationAPI} api - サインアップ の動作を変更するためのメソッドを提供するインターフェース。
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  api.user.setUserMetadata("screen_name", "username");
};
```

### 別のシステムのユーザー ID をユーザープロファイルに保存する

pre-user-registration Action を使用すると、別のシステムのユーザー ID をユーザープロファイルに保存できます。

```javascript lines expandable theme={null}
const axios = require('axios');

const REQUEST_TIMEOUT = 2000; // タイムアウトの例

/**
 * @param {Event} event - 登録を試みているユーザーとコンテキストに関する詳細。
 * @param {PreUserRegistrationAPI} api - サインアップの動作を変更するためのメソッドを備えたインターフェース。
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  try {
    // シークレット USER_SERVICE_URL = 'https://yourservice.com' を設定します
    const remoteUser = await axios.get(event.secrets.USER_SERVICE_URL, {
      timeout: REQUEST_TIMEOUT,
      params: {
        email: event.user.email 
      }
    });

    if (remoteUser) {
      api.user.setAppMetadata('my-api-user-id', remoteUser.id); 
    }
  } catch (err) {
    api.validation.error('custom_error', 'Custom Error');
  }
};
```

`axios` などの `npm` ライブラリを使用するには、そのライブラリを依存関係として Action に追加する必要があります。詳細については、[Write Your First Action](/docs/docs/ja-jp/customize/actions/write-your-first-action) の「依存関係を追加する」セクションを参照してください。

### 特定のJA3/JA4フィンガープリントからのアクセスを拒否する

`event.security_context` オブジェクトには、現在のトランザクションのJA3/JA4フィンガープリント値が含まれています：

```javascript lines theme={null}
/**
 * @param {Event} event - 登録しようとしているユーザーとコンテキストに関する詳細。
 * @param {PreUserRegistrationAPI} api - サインアップ の動作を変更するためのメソッドを提供するインターフェース。
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  const clientJa4 = event?.security_context?.ja4;
  console.log('[ACTION]', {clientJa4});
  const badFingerprints = ['t13d1517h2_8daaf6152771_b6f405a00624','t13d1516h2_8daaf6152771_d8a2da3f94cd'];
  if (clientJa4 && badFingerprints.includes(clientJa4)){
    api.access.deny('suspicious_tls_fingerprint', 'Your TLS fingerprint has been flagged as suspicious');
  }
};
```
