Android Login

This tutorial will show you how to integrate Lock v2 in your Android project in order to present a login screen.

Sample Project

Download this sample project configured with your Auth0 API Keys.

System Requirements
  • Android Studio 2.2
  • Android SDK 24
  • Emulator - Nexus 5X - Android 6.0
Show requirements

Add the Lock Dependency

Your first step is to add Lock into your project. Lock is a library for displaying a native UI in your app for logging in and signing up with different platforms via Auth0.

Add Lock with Gradle

Inside the build.gradle dependencies section:

apply plugin: 'com.android.application'
android {
  //..
}
dependencies {
  compile 'com.auth0.android:lock:2.0.0'   
}

Then, run Sync Project with Gradle Files inside Android Studio or ./gradlew clean assembleDebug from the command line.

For more information about Gradle usage, check their official documentation.

Configure the Manifest File

Add the following code to your project's AndroidManifest.xml.

<activity
    android:name="com.auth0.android.lock.LockActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:theme="@style/Lock.Theme">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="YOUR_AUTH0_DOMAIN"
            android:pathPrefix="/android/YOUR_APP_PACKAGE_NAME/callback"
            android:scheme="https" />
    </intent-filter>
</activity>

<activity android:name="com.auth0.android.lock.provider.WebViewActivity"/>

Add the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Do not add <android:noHistory="true"> to the LockActivity as this will alter the correct functionality of Lock.

Implement the Login

At this point, you're all set to implement the login in any activity you want. Inside the activity, add the onCreate, onDestroy, and callback methods.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Auth0 auth0 = new Auth0(YOUR_CLIENT_ID, YOUR_AUTH0_DOMAIN);
    lock = Lock.newBuilder(auth0, callback)
                    // Add parameters to the Lock Builder
                    .build(this);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    // Your own Activity code
    lock.onDestroy(this);
    lock = null;
}
private LockCallback callback = new AuthenticationCallback() {
    @Override
    public void onAuthentication(Credentials credentials) {
        // Login Success response
    }

    @Override
    public void onCanceled() {
        // Login Cancelled response
    }

    @Override
    public void onError(LockException error){
        // Login Error response
    }
};

Finally, whenever you want to start the login widget, call:

startActivity(lock.newIntent(this));
Mobile example screenshot

If you require in-depth configuration, see Lock Builder for more information

There are multiple ways of implementing the login dialog. What you see above is the default widget; however, if you want, you can use your own UI.

Optional: Log In with Social Connections

To have a simple login mechanism through social connections, all you have to do is enable them in your account's dashboard. Every social connection you switch on there, will appear in the login screen of your app.

Previous Tutorial
1. Introduction
Next Tutorial
3. Custom Login
Use Auth0 for FREECreate free Account