Configure a Custom Phone Provider with Twilio Verify

You can configure a custom phone provider with Twilio Verify, which helps fight fraud and protect user accounts. This sets up Twilio Verify as your custom phone provider.

Prerequisites

You must have a Twilio account with a valid SMS and/or phone delivery option.

Set up Twilio Verify service

You can create a Twilio Verify service through the Twilio Console or the Verify REST API. You will need the following to connect your Auth0 custom phone provider with Twilio Verify:

  • TWILIO_ACCOUNT_SID

  • TWILIO_AUTH_TOKEN

  • TWILIO_VERIFY_SID

Connect custom phone provider with Twilio Verify

  1. In the Auth0 Dashboard, navigate to Branding > Phone Provider. This takes you to the Phone Message Provider page.

  2. Select Custom as a Phone Provider.

  3. Under Provider Configuration, add the following code sample to allow the Twilio API to send phone messages to a user's phone number:

/**
* Handler to be executed while sending a phone notification
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {CustomPhoneProviderAPI} api - Methods and utilities to help change the behavior of sending a phone notification.
*/
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);

  // map auth0 voice value to call
  const messageType = event.notification.delivery_method === 'voice' ? 'call' : 'sms';

  const { recipient, code } = event.notification;

  // add this to fix " 1333444999  "
  // "333 444 5555"
  const sanitizedNumber = recipient.replace(/\s/g, '').trim();

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

};

Was this helpful?

/

3. Click the Key icon to open the Secrets menu. Add the following values from your Twilio Verify service setup:

  • TWILIO_ACCOUNT_SID

  • TWILIO_AUTH_TOKEN

  • TWILIO_VERIFY_SID

4. To load the Twilio helper library, click the Dependency icon. Then, click the Add Dependency button. When the Add Dependency pop-up window appears, enter the following:

  • Name: Twilio

  • Version: latest (should be auto populated)

When you click Create, Auth0 will search for the Twilio helper library and load the latest version.

5. Click Save. When you click Save, the Action automatically saves and deploys. To test the custom phone provider configuration before using it in a production environment, click Send Test Message.