Getting Started with Auth0 Android Library
Auth0 Library can be integrated into your Android apps to provide a beautiful way to log your users in and to sign them up in your app. It also provides social login support for providers like Facebook, Twitter, and Google.
1. Create a Client
If you haven’t already done so, create a new client application in your Auth0 dashboard and choose Native for the type.
2. Installation:
Configure Gradle
Simply add the below dependency to your build.gradle file
.
com.auth0.android:auth0:1.12.0’
Add Internet Permission
Add internet permission to your AndroidManifest.xml
file.
<uses-permission android:name=”android.permission.INTERNET” />
This will allow Auth0 make a network request.
Dashboard Configurations
The callback URL for your app in the Allowed Callback URLs
section is in Client settings.
For the sake of this post, the callback URL will look like this:
demo://YOUR_AUTH0_DOMAIN/android/{YOUR_APP_PACKAGE_NAME}/callback
3. Instantiate Auth0
Initialize Auth0 instant with your account details, which are the AUTH0_CLIENT_ID
and the AUTH0_DOMAIN.
Auth0 auth0 = new Auth0(“YOUR_CLIENT_ID”, “YOUR_AUTH0_DOMAIN”);
You can get this from your Auth0 dashboard — client→settings
.
OIDC Conformant Mode
Auth0 strongly encourages that Lock be used in OIDC Conformant mode. When this mode is enabled, it will force Lock to use Auth0’s current authentication pipeline and will prevent it from reaching legacy endpoints. By default is false.
//Configure the account in OIDC conformant modeaccount.setOIDCConformant(true);
The results of the AuthenticationCallback are in a credentials object. This object contains the tokens that you will require for authentication related operations in your app. See the tokens documentation for more specifics.
To ensure an Open ID Connect compliant response, you must either request an audience or enable the OIDC Conformant switch in your Auth0 dashboard under Client/ Settings/Advanced OAuth.
Authentication Callback
This callback helps you listen to authentication events.
private LockCallback callback = new AuthenticationCallback() {
@Override
public void onAuthentication(Credentials credentials) {
//User was Authenticated
}
@Override
public void onCanceled() {
//User pressed back
}
@Override
public void onError(LockException error)
//An Exception occurred
}
};
4. Show Lock widget
After you call the WebAuthProvider#start
function, the browser launches and shows the Lock widget. Once the user authenticates, the callback URL is called. The callback URL contains the final result of the authentication process.
WebAuthProvider.init(auth0)
.withScheme("demo")
.withAudience(String.format("https://%s/userinfo", getString(R.string.com_auth0_domain)))
.start(MainActivity.this, new AuthCallback() {
@Override
public void onFailure(@NonNull Dialog dialog) {
// Show error Dialog to user
}
@Override
public void onFailure(AuthenticationException exception) {
// Show error to user
}
@Override
public void onSuccess(@NonNull Credentials credentials) {
// Store credentials
// Navigate to your main activity
}
});
Waoooow. We did it.
Let your users feel safe with Auth0 authentication.
Project hosted on GitHub.
I hope this helped.