By Luciano Balmaceda
This quickstart demonstrates how to add user login to an Android application using Auth0.We recommend that you log in to follow this quickstart with examples configured for your account.New to Auth? Learn How Auth0 works, how it integrates with Native Applications and which protocol it uses.
Configure Auth0
Get Your Application Keys
When you signed up for Auth0, a new application was created for you, or you could have created a new one. You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.
When using the Default App with a Native or Single Page Application, ensure to update the Token Endpoint Authentication Method to
None
and set the Application Type to either SPA
or Native
.- Domain
- Client ID
If you download the sample from the top of this page, these details are filled out for you.
Configure Callback URLs
A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated. The callback URL for your app must be added to the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.If you are following along with the sample project you downloaded from the top of this page, you should set the Allowed Callback URL to
demo://{yourDomain}/android/YOUR_APP_PACKAGE_NAME/callback
.YOUR_APP_PACKAGE_NAME
with your application’s package name, available as the applicationId
attribute in the app/build.gradle
file.
Configure Logout URLs
A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in thereturnTo
query parameter. The logout URL for your app must be added to the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be unable to log out from the application and will get an error.
If you are following along with the sample project you downloaded from the top of this page, the logout URL you need to add to the Allowed Logout URLs field is
demo://{yourDomain}/android/YOUR_APP_PACKAGE_NAME/callback
.YOUR_APP_PACKAGE_NAME
with your application’s package name, available as the applicationId
attribute in the app/build.gradle
file.
Install the Auth0 Android SDK
Add the Auth0 Android SDK into your project. The library will make requests to the Auth0’s Authentication and Management APIs.Add Auth0 to Gradle
In your app’sbuild.gradle
dependencies section, add the following:
Sync Project with Gradle Files
Remember to synchronize using the Android Studio prompt or run./gradlew clean build
from the command line. For more information about Gradle usage, check their official documentation.intent-filter
that captures the authentication callback URL. For this, the Auth0 tenant domain and the scheme that take part in the callback URL must be set.
We’ve used a value of
demo
for auth0Scheme
here, so that a custom URL scheme can be used for the URL that Auth0 redirects to after login. Whenever possible, Auth0 recommends using Android App Links with https
as a secure way to link directly to content within your app. Custom URL schemes can be subject to client impersonation attacks. You can read more about setting this value in the Auth0.Android SDK readme.You do not need to declare a specific
intent-filter
for your activity, because you have defined the manifest placeholders with your Auth0 Domain and Scheme values and the library will handle the redirection for you.android.permissions.INTERNET
permission is specified in the AndroidManifest.xml
file:
./gradlew clean assembleDebug
from the command line.
For more information about using Gradle, check the Gradle official documentation.
Add Login to your App
Universal Login is the easiest way to set up authentication in your application. We recommend using it for the best experience, best security and the fullest array of features. In theonCreate
method, create a new instance of the Auth0
class to hold user credentials:
We suggest you do not hardcode the values for
clientId
and domain
as you may need to change them in the future. Instead, use String Resources, such as @string/com_auth0_domain
, to define the values.loginWithBrowser
method and use the WebAuthProvider
class to authenticate with any connection you enabled on your application in the Auth0 dashboard. Here, you can pass the scheme value that was used in the auth0Scheme
manifest placeholder as part of the initial configuration:
WebAuthProvider#start
function, the browser launches and shows the login page. Once the user authenticates, the callback URL is called. The callback URL contains the final result of the authentication process.
There are many options to customize the authentication with the 
WebAuthProvider
builder. You can read about them in the Auth0 SDK for Android documentation.
Checkpoint
Add a button to your application that callsloginWithBrowser
. When you click it, verify that your Android application redirects you to the Auth0 Universal Login page and that you can now log in or sign up using a username and password or a social provider.Once that’s complete, verify that Auth0 redirects back to your app.Add Logout to your App
UseWebAuthProvider
to remove the cookie set by the Browser at authentication time, so that the users are forced to re-enter their credentials the next time they try to authenticate.
Add a logout
method to your app to remove the user’s session and log them out of the app. Here, you can pass the scheme value that was used in the auth0Scheme
manifest placeholder as part of the initial configuration:
WebAuthProvider
class. This call will open the Browser and navigate the user to the logout endpoint. If the log out is cancelled, you might want to take the user back to where they were before attempting to log out.
Checkpoint
Add a button to your app that callslogout
and logs the user out of your application. When you click it, verify that your Android app redirects you logout page and back again, and that you are no longer logged in to your application.Show User Profile Information
Use theAuthenticationAPIClient
class to retrieve the users profile from Auth0. This requires:
- The access token as received during the login phase
- The
profile
scope to be included whenWebAuthProvider.login
is called
email
scope must also be specified if the user’s email address is to be retrieved.
This quickstart sets the
openid profile email
scopes by default during the login step above.Checkpoint
Call theshowUserProfile
function after login and verify that the user’s profile information has been returned in the onSuccess
callback.