Sign in with Facebook
In this guide, you will learn how to login using Facebook Login and Parse User class through Back4App. This tutorial uses a basic app created in Android Studio 4.1.1 with buildToolsVersion=30.0.3 , Compile SDK Version = 30 and targetSdkVersion 30
At any time, you can access the complete Android Project built with this tutorial at our Github repositories
Create a Login with Facebook feature to your Android App using Parse and Back4App.
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
To start using Facebook functions, you need to:
- Follow Facebook’s Quickstart Guide by clicking here and pay attention to the following recommendations:
Here are the steps included in Facebook’s Quickstart Guide, which you need to follow carefully, as you are not going to follow them precisely as Facebook suggests:
- In Step 3 instead of adding
in the dependencies{} section at build.gradle (Module:app), add the following code in the dependencies{} section at build.gradle (Module:app)
- Remember to update the version of Parse Facebook Utils SDK for Android to the latest one. You can find out which is the latest version at the JitPack website, following these steps:
- At JitPack website paste parse-community:ParseFacebookUtils-Android in the Git repo URL box.
- After doing that, click on the Look up button. Then you should see the available versions of Parse SDK for Android, as shown in the following image.
- In Step 4, you’ll be asked to add internet permission at the application element in /app/manifest/AndroidManifest.xml file, but you have already added it while following Install Parse SDK Tutorial so you don’t need to do this.
- In Step 6, you’ll need to provide key hashes, so you must have Open SSL installed. Facebook’s Guide doesn’t provide command lines to generate key hashes in Linux, but doing that is simple, as all it requires from you is to open a terminal window and run the following command:
- Don’t follow the steps of Facebook’s Quickstart Guide right after Step 6.
Whats is SHA-1 (Secure Hashing Algorithm)
SHA-1, called the Secure Hashing Algorithm, is the most common encryption algorithm. SHA-1, designed by United States National Security Agency. SHA-1 fingerprint is a unique key generated for your PC that can be used for signing. Its mainly used for submitting for using the some APIs (Like the Facebook api we will use in this guide.). If you want to learn more details, you can visit the SHA-1 Wikipedia page.
You should follow the other steps described on Facebook’s Quickstart Guide and not mentioned here.
- Find the “Facebook Login” block and click on Settings. The “Facebook Login” block looks like this:
3. We need to add our facebook_id, which we got from Facebook Guide to string.xml(You should have followed the Facebook Guide and did this before). Go to your Android Studio Project, open your strings file: .../app/src/main/res/values/strings.xml, copy your facebook_app_id and paste it in the Facebook appId field of the last page of Back4App that you opened. Lastly, press the + button.
Go back to your Android Studio Project and inside the application element in /app/manifest/AndroidManifest.xml file, right after the meta data element, add the following code.
Don’t forget to put your Facebook App ID in the code above.
In your Android Studio Project, in the Java file called App that extends Application that you created to initialize the Parse SDK, on its onCreate method, right after Parse.initialize() call, use the following code to initialize Parse Facebook Utils SDK.
If you don’t have an App.java file as described in this step, access the Install Parse SDK for Android documentation and make sure that you have followed all the steps required to install Parse SDK correctly. If you do not install Parse SDK properly your facebook login with Parse will not work.
At the beginning of each Parse activity, import the following:
To implement your Login Activity, do the following:
- Import into your LoginActivity, in addition to the dependencies imported in Step 4:
2. To implement Facebook Login, simply use the following code:
In the example project, this code is placed inside a LOGIN VIA FACEBOOK button callback.
3. After a successful login through Facebook to our App, we can now get some basic logged user information.. As you can see, there are many more methods included in the code above. The getUserDetailFromFB method is responsible for fetching user details. Here’s the code for this method:
4. It’s interesting to add a method to display Alert Dialogs and make the process look more professional. In this function, we also get a user parameter. When going to the MainAtivity page, we send this user parameter in the intent, and in the MainActivity, we pull the information in this user and print it on the screen. The method below does this:
5. If you want to associate an existing ParseUser to a Facebook account, you can link it like so:
In the example project, this code is placed inside a LINK YOUR ACCOUNT TO FACEBOOK button callback.
6. If you want to unlink Facebook from a user, simply do this:
In the example project, this code is placed inside a UNLINK YOUR ACCOUNT FROM FACEBOOK button callback.
7. It’s very important to use the following as a method outside the onCreate() method of your LoginActivity to pass login results to the LoginManager via callbackManager and avoid errors.
To implement Facebook Logout, simply use the code mentioned below:
In the example project, this code is placed inside a LOG OUT button callback.
The method alertDisplayer is the same that you added in theLoginActivity, don’t forget to change its Intent arguments though.
At this stage, you can log in, register and log out of your app with Facebook using Parse Server core features through Back4App!