Authenticate users with a one-time code via e-mail

With the e-mail connection, the user is requested to enter their e-mail address. Auth0 then sends an email to that address containing a one-time code.

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

If the e-mail address 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. Optional: Configure an email provider

By default, Auth0 sends the email from its own messaging provider. Optionally, you can configure your own email provider to better monitor and troubleshoot the email service.

2. Configure the connection

In the Dashboard, on the Email page, under Connections > Passwordless, you can configure the contents and behavior of the email.

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: {{ code }}
{% endif %}

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

The following parameters are available when defining the template:

Exposed Parameter Description
code the password to use
link the magic link
application.name the name of the application name where the user is signing up
request_language the requested language for the message content
operation equals change_email when the template is triggered by an update to a user's email via the API, otherwise null

Implementation

Using Auth0 Lock

Sample Project

Download this sample project configured with your Auth0 API Keys.

The Lock is a widget that allows you to easily integrate Auth0's Passwordless Authentication into your Android applications.

After installing and configuring Lock.Android you will be able to use Lock as follows.

Begin by adding the Lock e-mail library to your build.gradle file:

compile 'com.auth0.android:lock-email:1.10.+'

Once the user is successfully authenticated, LockActivity will send an Action using LocalBroadcasterManager and then end itself by calling finish(). The activity that will receive this Action and will show Lock needs to register a listener in the LocalBroadcastManager:

// This activity will show Lock
public class HomeActivity extends Activity {
  private LocalBroadcastManager broadcastManager;

  private BroadcastReceiver authenticationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      UserProfile profile = intent.getParcelableExtra(Lock.AUTHENTICATION_ACTION_PROFILE_PARAMETER);
      Token token = intent.getParcelableExtra(Lock.AUTHENTICATION_ACTION_TOKEN_PARAMETER);
      Log.i(TAG, "User " + profile.getName() + " logged in");
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Customize your activity

    broadcastManager = LocalBroadcastManager.getInstance(this);
    broadcastManager.registerReceiver(authenticationReceiver, new IntentFilter(Lock.AUTHENTICATION_ACTION));
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    broadcastManager.unregisterReceiver(authenticationReceiver);
  }
}

Then just call the activity:

Intent emailIntent = new Intent(this, LockEmailActivity.class);
startActivity(emailIntent);

When this code runs, it will ask the user for their email address:

Then Auth0 will send to the user an email containing the one-time code:

Lastly, the user enters the one-time password into Lock. Then, if the password is correct, the user is authenticated.

Using your own UI

If you choose to build your own UI, first ask the user for their email address. Then call requestEmailVerificationCode on the APIClient

final APIClient client = lock.getAPIClient();
client.requestEmailVerificationCode(email, new BaseCallback<Void>() {
    @Override
    public void onSuccess(Void payload) {
        // Email sent, continue to next page.
    }

    @Override
    public void onFailure(Throwable error) {
        // Error sending email.
    }
});

Once the passwordless login process begins, ask the user for the one-time code. Then authenticate using that code:

final APIClient client = lock.getAPIClient();
client.emailLogin(email, passcode, authenticationParameters, new AuthenticationCallback() {
    @Override
    public void onSuccess(UserProfile userProfile, Token token) {
        // Login success, you can now use the userProfile or token
    }

    @Override
    public void onFailure(Throwable throwable) {
        // Login failed.
    }
});

Instead of sending the user a one time code, you can configure Auth0 to send a magic link:

Auth0 will send an email containing a clickable button or link:

After clicking the button, the user will be automatically signed-in to your application:

The next version of the Android library will also support this feature.