By Luciano Balmaceda
This tutorial demonstrates how to add user login to an Android application using native Facebook Login.We recommend that you log in to follow this quickstart with examples configured for your account. This tutorial describes how to implement login with the Facebook SDK.Before You Start
- Install and configure the Facebook Login SDK. You’ll also go through the process of creating a Facebook app in https://developers.facebook.com. When you finish this step, you should have a mobile app running with Facebook Login integrated.
- Configure your Auth0 application in the dashboard to use Facebook Native Sign In. See Add Facebook Login to Native Apps. When you finish this step, your application will be able to implement Facebook Native Login.
Set up the “Continue with Facebook” button
This guide will help you add authentication with Auth0 to the application you built in the first step.Request Facebook permissions
Your application is already able to sign in with Facebook. However, to ensure you have a rich user profile, you need to update the permissions with which the Facebook Login Button was set up. Set the requested permissions topublic_profile
and email
. This way, the user email will also be included as part of the response, provided the access request is accepted by the user.
performLogin
and the interface SimpleCallback
. Go ahead and add both.
onSuccess
method.
Integrate Facebook
When you sign in with Facebook at Auth0, the backend will perform some checks in the background to ensure the user is who they say they are. To achieve this, it needs to be provided with a Session Access Token. Furthermore, if a user needs to be created on Auth0 to represent this Facebook user, the backend will require some of their information, such as their name, last name, and email. The email, if provided, will be flagged as non-verified on the Auth0 user profile. To obtain the Session Access Token and the user profile, two additional requests need to be made against the Facebook API.Fetch Facebook session Access Token
Make a new GET request against the Facebook API’s/oauth/access_token
endpoint.
Use the following query parameters:
grant_type
:fb_attenuate_token
.fb_exchange_token
: the access token received upon login.client_id
: your App ID. This value comes from the Facebook Developer’s dashboard and should already be in use in your application if you have integrated Facebook Login successfully.
GraphRequest
class to perform this request.
Fetch Facebook user profile
Now make another GET request, just like in the step above. The endpoint path will be the User ID value from the Facebook login result (for example,/904636746222815
).
Use the following parameters:
access_token
: the access token received upon login.fields
: the fields from the user profile that you’d like to get back in the response. These are directly tied to the Facebook Login Button permissions that were configured at the beginning. When a permission is optional, the user must first consent to give access to it. For the purpose of signing up a user at Auth0, their full name and email will suffice.
Integrate Auth0
Now that the required artifacts have been obtained, you are ready to trade them for Auth0 user credentials, such as the ID and Access Tokens. But first, you must set up the Auth0 SDK to make that last request.Get your application keys
Go to the Applications section of the Auth0 Dashboard and select the existing application in which you enabled Sign in with Facebook. If you need help with this step, please check the requirements section at the top of this article. Copy the Domain and Client ID values from the application settings page. These are required by the SDK. Create two new resources in your Android application’sstrings.xml
file to store them. The name of the keys must match the ones used below:
Install the Auth0 SDK
In your Android application, add this line to theapp/build.gradle
file:
Web Authentication
If your application does not plan to make use of the Web Authentication module provided by the SDK, you will need to remove the unused activity from theAndroidManifest.xml
file to prevent Manifest Placeholder issues. This can be achieved by adding an activity declaration and annotating it with tools:node="remove"
.Exchange the received data for Auth0 tokens
The SDK must be instantiated before use. Define a field at the class level and initialize it on theonCreate
method. Note how the credentials defined in the step above are passed to the Auth0
constructor and then a new instance of the AuthenticationAPIClient
is created with it.
exchangeTokens
.
The API client declares the method loginWithNativeSocialToken
that receives a token and a subject type. The former corresponds to the session token, and the latter indicates what type of connection the backend will attempt to authenticate with. For native Facebook Login, you will use the following value:
user_profile
key) and the scope you request the Auth0 tokens contain.
It’s a good practice to keep all the values that you know won’t change as constants at the top of the class. The sample makes use of constants for the subject token type, the Facebook permissions, and the Auth0 scopes.
You can read more about Auth0 scopes in the dedicated article.
performLogin
method.
Edit on GitHub