Android Authorization

This step demonstrates how to use Auth0 to create access roles for your users. With access roles, you can authorize or deny content to different users based on the level of access they have.

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • Android Studio 2.3
  • Android SDK 25
  • Emulator - Nexus 5X - Android 6.0
Show requirements

Before Starting

Be sure that you have completed the user profile quickstart.

Create A Rule To Assign Roles

First, you need to create a rule that assigns your users either an admin role, or a single user role. To do so, go to the new rule page and select the "Set Roles To A User" template, under Access Control. Then, replace this line from the default script:

if ('') > -1)

to match the condition that fits your needs.

By default, it says that if the user email contains then they will be given an admin role, otherwise a regular user role.

You can define more roles other than admin and user, depending on your product requirements.

In the demo app, we use to validate, like the next rule:

  var addRolesToUser = function(user, cb) {
    if ('') > -1) {
      cb(null, ['admin']);
    } else {
      cb(null, ['user']);

Test the Rule in Your Project

Once you have the user profile (as explained in the user profile tutorial), you can save it and access it at any point.

Inside it, you will have the role, and you will be ready to perform the access control.

// app/src/main/java/com/auth0/samples/activities/
List<String> roles = (List<String>) mUserProfile.getAppMetadata().get("roles");

if (roles.contains("admin")) {
 // perform any action

Notice that you'll find the roles information within the appMetadata HashMap and not in the userMetadata. Application metadata cannot be modified by users, whereas User metadata can be.

Restrict Content Based On Access Level

At this point, you are able to distinguish the users roles in your app and authorize or deny (depending on the user) access to a certain feature.

Previous Tutorial
6. Calling APIs
Use Auth0 for FREECreate free Account