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

# Twilio Verify でカスタム電話プロバイダーを設定する

> カスタム電話プロバイダー Action を使用して Twilio Verify を Auth0 に統合し、MFA とパスワードレス SMS コードを Twilio Verify API 経由でルーティングします。

<Steps>
  <Step title="前提条件">
    * 有効な SMS および/または音声通話による配信オプションを備えた [Twilio](https://twilio.com/) アカウントが必要です。

    * [Twilio Console](https://www.twilio.com/console/verify/services) または [Verify REST API](https://www.twilio.com/docs/verify/api/service#create-a-verification-service) を使用して、[Twilio Verify](https://www.twilio.com/docs/verify) サービスを作成する必要があります。
  </Step>

  <Step title="カスタム電話メッセージプロバイダーを設定する">
    [Auth0 Dashboard > Branding > Phone Provider](https://manage.auth0.com/dashboard/#/phone/templates/phone/provider) で、**Phone Provider** の下にある **Custom** を選択します。

    **Provider Configuration** で、Twilio API がユーザーの電話番号に電話メッセージを送信できるよう、次のコードサンプルを追加します。

    ```javascript lines expandable theme={null}
    /**
    * 電話通知の送信時に実行されるハンドラー
    * @param {Event} event - ユーザーおよびログイン時のコンテキストに関する詳細情報。
    * @param {CustomPhoneProviderAPI} api - 電話通知の送信動作を変更するためのメソッドとユーティリティ。
    */
    exports.onExecuteCustomPhoneProvider = async (event, api) => {
     const { TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_VERIFY_SID } = event.secrets;

      const client = require('twilio')(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);

      // auth0 の voice 値を call にマッピング
      const messageType = event.notification.delivery_method === 'voice' ? 'call' : 'sms';

      const { recipient, code } = event.notification;

      // " 1333444999  " や "333 444 5555" を修正するために追加
      //
      const sanitizedNumber = recipient.replace(/\s/g, '').trim();

      await client.verify.v2.services(TWILIO_VERIFY_SID)
        .verifications.create({
          to: sanitizedNumber,
          channel: messageType,
          customCode: code
        })

    };
    ```
  </Step>

  <Step title="シークレットを追加する">
    **Key** アイコンを選択して **Secrets** メニューを開きます。Twilio Verify service の設定から、次の値を追加します。

    * `TWILIO_ACCOUNT_SID`
    * `TWILIO_AUTH_TOKEN`
    * `TWILIO_VERIFY_SID`
  </Step>

  <Step title="Twilio ヘルパーライブラリを読み込む">
    **Dependency** アイコンを選択し、**Add Dependency** を選択します。開いた **Add Dependency** ウィンドウで、次の値を入力します。

    * **Name**: `twilio`
    * **Version**: `latest`

    **Create** を選択すると、Auth0 が Twilio ヘルパーライブラリを検索し、最新バージョンを読み込みます。
  </Step>

  <Step title="デプロイとテスト">
    **Save** を選択して、Action を保存してデプロイします。

    本番環境で使用する前にカスタム電話プロバイダーの設定をテストするには、**Send Test Message** を選択します。
  </Step>
</Steps>
