Authenticate users with a one-time code via SMS in a SPA

With the SMS connection, the user is requested to enter a phone number. Auth0 then uses Twilio to send a one time code to that number.

Once the user enters this code into your application, a new user will be created in the sms connection. The user is then authenticated by Auth0.

If the phone number matches an existing user, Auth0 just authenticates the user:

On mobile platforms, your application will receive an id_token, the user profile and, optionally, a refresh_token.

Setup

1. Open an account with Twilio

You will need a Twilio Account SID and a Twilio Auth Token. These are the Twilio API credentials that Auth0 will use to send an SMS to the user.

2. Configure the connection

In the Dashboard under Connections > Passwordless, set the SMS slider to the right to enable the SMS Passwordless feature.

Enter your Twilio Account SID and Twilio Auth Token in the appropriate fields.

NOTE: For information on obtaining a Twilio SID and Auth Token, see: How to create an Application SID and Auth Tokens and how to change them.

Select the SMS Source that users will see as the sender of the SMS.

NOTE: For information on using Copilot, see: Sending Messages with Copilot.

Enter either your Twilio Messaging Service SID or a From phone number, depending on the SMS Source selected above.

Lastly, enter the Message that will appear in the body of the SMS.

NOTE: The @@password@@ placeholder in the Message will be automatically replaced with the one-time password that is sent to the user.

Click SAVE.

Multi-Language Support

The Message area supports usage of multiple languages.

By making a call to the /passwordless/start authentication endpoint, you can set the value of an 'x-request-language' header to the language of your choice. If the value of this header is not set, the language will be extracted from the value in the 'accept-language' header that is automatically set by the browser.

The Message area accepts Liquid syntax. You can use this syntax, combined with exposed parameter values, to programmatically construct elements of the message. For example, you can reference the request_language parameter to change the language of the message:

{% if request_language contains 'dutch' %}
   Hier is uw verificatie code: {{ password }}
{% endif %}

{% if request_language contains 'fr-FR' %}
   Ceci est votre code: {{ password }}
{% endif %}

The following paramaters are available when defining the template:

Exposed Parameter Description
password or code the password to use
phone_number the user's phone number
application.name the name of the application name where the user is signing up
request_language the requested language for the message content

3. Enable your apps

Go to the Apps tab of the SMS settings and enable the apps for which you would like to use Passwordless SMS.

4. Configure CORS

For security purposes, you must add your app's origin URL to the list of Allowed Origins (CORS) in your app's Settings Section of the Dashboard, unless this origin URL has been already added to the Allowed callback URLs list.

Implementation

Use Auth0 UI widget (Lock)

Sample Project

Download this sample project configured with your Auth0 API Keys.

Auth0 Lock Passwordless is a professional-looking dialog that allows users to log in after receiving a one-time password via email or text message.

After installing Lock Passwordless, you must initialize it with your Client Id and domain. You can find this information on your application settings page.

You can then trigger the login widget with the following code:

<script src="https://cdn.auth0.com/js/lock-passwordless-2.2.min.js"></script>
<script type="text/javascript">
  function login(){
    // Initialize Passwordless Lock instance
    var lock = new Auth0LockPasswordless('YOUR_CLIENT_ID', 'YOUR_AUTH0_DOMAIN');
    // Open the lock in Email Code mode with the ability to handle
    // the authentication in page
    var appearanceOpts = {
      autoclose: true
    };
    // Open the lock in SMS mode with the ability to handle the authentication in page
    lock.sms(appearanceOpts,function (error, profile, id_token, access_token, state, refresh_token) {
      if (!error) {
        //usually save profile and id_token
      }
    });
  }
</script>
<a href="javascript:login()">Login</a>

This will open a dialog that asks the user for their phone number.

Then Auth0 will use Twilio to send to the user an SMS containing the one-time code:

SMS one-time code

Lock will ask for the code that has been sent via SMS to the provided number. The code can then be used as a one-time password to log in:

If the code is correct, the user will be authenticated. This will call the callback of the lock.sms function where the id_token, refresh_token and user profile are typically stored. Then the user will be allowed to continue to the authenticated part of the application.

Use your own UI

Sample Project

Download this sample project configured with your Auth0 API Keys.

You can perform passwordless authentication in your SPA with your own custom UI using the Auth0 JavaScript client library.

Construct a new instance of the Auth0 client as follows:

<script src="https://cdn.auth0.com/w2/auth0-7.4.min.js"></script>
<script type="text/javascript">
  'use strict';
  const auth0 = new Auth0({
    clientID: 'YOUR_CLIENT_ID',
    domain: 'YOUR_AUTH0_DOMAIN'
  });
  //...
</script>

You must provide a way for the user to enter a phone number to which the SMS will be sent. Then you can begin the passwordless authentication as follows:

function sendSMS(){
  var phoneNumber = $('input.phone-number').val();
  auth0.requestSMSCode({ phoneNumber:phoneNumber }, function(err) {
    if (err) {
      alert('error sending SMS: '+ err.error_description);
      return;
    }
    // the request was successful and you should
    // receive the passcode to the specified phone
    $('.enter-phone').hide();
    $('.enter-code').show();
  });
}

This will send an SMS to the provided phone number. The user must now enter the code into your custom UI. Then you can continue with the login as follows:

function login(){
  var phone = $('input.phone-number').val();
  var code = $('input.code').val();
  //submit the passcode to authenticate the phone
  auth0.verifySMSCode({ phoneNumber: phone, code: code }, function (err, profile, id_token, access_token) {
    if (err){
      alert('Couldn\'t login ' + err.message);
    } else {
      // the authentication was successful
      // you can use profile, id_token and access_token
      localStorage.setItem('userToken', id_token);
      alert('Welcome ' + profile.name );
    }
  });
};