Step 1. Set Up the Application
Sample Project
Download a sample project specific to this tutorial to get started. Download Fork on Github System Requirements- Android Studio 2.3
- Android SDK 25
- Emulator - Nexus 5X - Android 6.0
Set the Dependencies
For this implementation, we will use the following dependencies within the app’sbuild.gradle
file:
- Auth0.Android: this package enables integration with Auth0 to authenticate users.
- OkHttp: this package provides an HTTP application to make requests to the Node.JS API.
- JWTDecode.Android: this package will assist with decoding .
- AppCompat: this package lets us use the toolbar widget for navigation in our activities.
Update the Manifest
Open the application’sAndroidManifest.xml
and add the internet permission:
Set Configuration Values
Set your Auth0 , Auth0 Domain, and API’s url in thestrings.xml
resource located in /res/values/strings.xml
:
activities/
: this package will contain theLoginActivity.java
,TimeSheetActivity.java
,FormActivity.java
, andUserActivity.java
.models/
: this package will contain theTimeSheet.java
andUser.java
data models.utils/
: this package will contain theUserProfileManager.java
,TimeSheetAdapter.java
, andImageTask.java
Step 2. Authorize the User
Update the Manifest
Open the app’sAndroidManifest.xml
and add the LoginActivity
:
Create the Login Activity
TheLoginActivity
will handle user authorization and be the initial screen users see. We’ll create a login()
method to initialize a WebAuthProvider
and start authentication. Ensure you provide the correct scheme, , and scope to the WebAuthProvider
. For this implementation we will use:
- scheme:
demo
- audience:
https://api.exampleco.com/timesheet
s (the Node.JS API) - response_type:
code
- scope:
create:timesheets read:timesheets openid profile email offline_access
. These scopes will enable us toPOST
andGET
to the Node.JS API, as well as retrieve the user profile and a .
login()
method, upon successful authentication we’ll redirect the user to the TimeSheetActivity
.
Store the Credentials
To store the credentials received after login, we’ll use theCredentialsManager
from the Auth0.Android library and SharedPreferences for storage.
Before initializing the WebAuthProvider
in the login()
method, we can create the CredentialsManager
. Passing an AuthenticationAPIClient
to the CredentialsManager
enables it to refresh the if they are expired.
login()
method so that credentials are stored via the CredentialsManager
after a successful authentication.
Step 3. Get the User Profile
Create the User Model
Create a simple User model that will be utilized by theUserProfileManager
and UserActivity
.
Store the User Profile
To handle storing user profile information we’ll create a manager classUserProfileManager
. The UserProfileManager
will use SharedPreferences to store data.
login()
method in the LoginActivity
to retrieve the and get the user profile from the token with the JWTDecode.Android library. Then store the user profile with the UserProfileManager
.
Step 4. Display UI Elements Conditionally Based on Scope
To determine whether a user has permissions to perform certain actions, we can look at thescope
that was granted to the user during the authentication process. The scope
will contain a string with all the scopes granted to a user, so to determine whether a particular scope was granted, we simply need to look whether the string of scopes contain the substring for that particular scope.
Store the Scope
First, we can update theUser
class to store the granted scopes, and then provide a helper method, hasScope()
which can be used to determine whether the granted scopes contain a particular scope:
UserProfileManager
to store the extra field:
LoginActivity
to pass along the scope
so it can be stored in the User
object:
Display Approval Menu Based on Scope
Now, we can display certain UI elements based on whether the user was granted a particular scope. As an example, we have an approval menu item which should only be visible to users who have been granted theapprove:timesheets
scope.
Below you can see the code from the BaseActivity
class which checks whether a user has the approve:timesheets
scope, and based on that will set the visibility of the menu item which displays the approval activity:
Step 5. Call the API
Update the Manifest
Open the app’sAndroidManifest.xml
and add the TimeSheetActivity
:
Create the Timesheets Activity Layouts
Next createtimesheet_activity.xml
, the layout for the TimeSheetsActivity
:
ListView
widget will contain individual entries which are represented by the item_entry.xml
layout:
TimeSheetActivity
, we’ll create the timesheet_action_menu.xml
menu resource (/res/menu/
):
Create the Timesheet Model
Create a model for working with timesheet data in our views:Create the Timesheet Adapter
TheTimeSheetAdapter
is a utility class which will take an array of timesheet entries and apply them to the ListView
on the TimeSheetActivity
.
Create the Timesheet Activity
TheTimeSheetActivity
displays the timesheet entries for the logged in user which are stored on the server.
- The
@string/api_url
is set tohttp://10.0.2.2:8080/timesheets
so the Android Emulator can connect to the Node.JS API running onhttp://localhost:8080
. - The
callAPI()
method retrieves timesheets from the Node.JS API. - The
processResults()
method takes the JSON response fromcallAPI()
and converts itTimeSheet
objects. - The
onCreateOptionsMenu(
) andonOptionsItemSelected(
) methods handle the Toolbar widget navigation functionality.
Step 6. View the User Profile
To display the logged in user’s profile we’ll create theUserActivity
, a corresponding user_activity.xml
layout, and the user_action_menu.xml
for the Toolbar navigation. The view will display the user’s name, email, and profile picture.
Update the Manifest
Open the app’sAndroidManifest.xml
and add the UserActivity
:
Create the User Activity Layouts
Next createuser_activity.xml
, the layout for the UserActivity
, with an ImageView
for the profile picture and TextViews
for the user’s name and email.
user_actions_menu.xml
for the UserActivity
Toolbar:
Load the Profile Picture from URL
In order to load the user profile picture from the URL, create a task which extendsAsyncTask
and executes in the background.
Create the User Activity
In theonCreate()
method we’ll retrieve the user information from the UserProfileManager
and set the values in the view. As before, the onCreateOptionsMenu()
and onOptionsItemSelected()
methods handle the Toolbar widget navigation functionality.
Step 7. Form for New Timesheets
Next create theFormActivity
and layout to handle creating new timesheet entries.
Update the Manifest
Open the app’sAndroidManifest.xml
and add the FormActivity
:
Create the Form Activity Layouts
Create theform_activity.xml
layout with EditText
for the project name and hours worked inputs, and a DatePicker
for the day worked.
form_actions_menu.xml
for the FormActivity
Toolbar:
Create the Form Activity
- The
@string/api_url
is set tohttp://10.0.2.2:8080/timesheets
so the Android Emulator can connect to the Node.JS API running onhttp://localhost:8080
. - The
onCreate()
method initializes the form and collects the input for thepostAPI()
method when the submit button is pressed. - The
postAPI()
method will send the user input retrieved from the form to the Node.JS API in JSON format. - The
clearForm()
method clears the input forms. - The
onCreateOptionsMenu()
andonOptionsItemSelected()
methods handle the Toolbar widget navigation functionality.
Test the App
Before continuing, ensure you have implemented the Node.JS API.- Start the API by navigating to the API’s directory in your terminal and entering the
node server
command. - Open the mobile app in Android Studio and press the Run button.
- Select the Nexus 5X API 23 virtual device.
- Once the emulator has loaded the mobile app, you can login user then, create and view timesheet entries from the running Node.JS API.