Getting Started with Auth0 Android Library
Auth0 Library can be integrate into your Android apps to provide a beautiful way to log your users in and to sign them up in your app. Its 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 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 encouraged 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 responses 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 the authentication events
private LockCallback callback = new AuthenticationCallback() {@Overridepublic void onAuthentication(Credentials credentials) {//User was Authenticated}@Overridepublic void onCanceled() {//User pressed back}@Overridepublic 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() {@Overridepublic void onFailure(@NonNull Dialog dialog) {// Show error Dialog to user}@Overridepublic void onFailure(AuthenticationException exception) {// Show error to user}@Overridepublic 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.