How to connect Facebook Auth to React Native App in 3 easy steps
Author: Vira Rodionova (React Native Developer)
Hi! This post describes Facebook’s authentication process in a React Native mobile application.
The purposes of this post are: providing a step-by-step tutorial about Facebook authentication connection to the React Native application, a description of the process of interacting with the server, some of the pitfalls on the way and my groundwork about the case.
However, before starting, let’s consider a diagram of the mobile application interaction with Facebook services. It will allow us to understand why we perform particular actions.
The sequence of interaction is as follows:
1. A user initiates the action that is associated with Facebook services (in the post we will have authentication)
2. The mobile application sends a request to Facebook services to perform an action. The following data is attached to the request:
— the type of action to be performed
— APP ID
, which corresponds to the Facebook application ID
— ID
of the source code of the React Native application (packageName
/ bundleId
for Android / iOS, respectively)
3. Facebook verifies the request with a pair of values: APP ID
, packageName
/ bundleId
4. After successful verification, Facebook processes the requested action.
5. And Facebook returns to the mobile application response with the result.
A brief description of the authentication setup process
To configure our application, you must perform the following sequence of actions:
1. Setting up the application on Facebook
1.1. Create a Facebook Developer Account
1.2. Create a Facebook application
1.3. Register platforms on Facebook
2. Configuring React Native Applications
2.1. [Android] Connecting Facebook SDK and setting up Facebook APP ID in app code
2.2. [iOS] Connecting Facebook SDK and setting up Facebook APP ID in app code
3. Configuring Facebook Login
3.1. Connecting Facebook Login product
3.2. Advanced settings for Android
3.3. An example of getting a token through the Login Manager
1. Setting up the application on Facebook
To use the “products” provided by Facebook, for example, authorization, advertising, in-app purchases etc. — you need to create an application in “Facebook for Developers”. After creating the application, you will receive a unique ID, which is required to identify your React Native application on Facebook.
1.1. Create a Facebook Developer Account
You need to have a Facebook account to create applications on “Facebook for developers”. You can use your personal one, but I advise you to register a separate one. By default, “Facebook for developers” is available for all accounts. So, you can start creating an application.
1.2. Create a Facebook app
- Go to Facebook Dashboard page and click on the
Add a New App
button. - In the window that appears, enter the required information and click on the
Create App ID
button. - Facebook will ask you to choose a usage scenario, but we can skip this step.
Now we have an APP ID
for your React Native application to access Facebook.
1.3. Register platforms on Facebook
Also, Facebook must know about those applications that can access it. Therefore, in the settings of the Facebook application, you should also specify the ID of your React Native application. Facebook works directly with native applications, so you need to register an ID for each platform. It ensures the uniqueness of the application that wants to use Facebook services. Application distribution platforms (Play Market / AppStore) do not allow the appearance of two applications with identical identifiers ( packageName
/ bundleId
).
To add a platform, open the Facebook App Settings -> Basic and find the “Add platform” button at the bottom of the page:
When the application is created in the AppStore, you will need to update the field: iPhone Store ID
How to find Android packageName
/app/src/main/AndroidManifest.xml
<manifest package="com.reactnativeads" ...>
How to find iOS bundleId
2. Configuring React Native Applications
The first thing you need to connect to the React Native application if you decide to use Facebook services is the Facebook SDK. You should also “register” the APP ID
, which we received in step 1.2, in the configuration files for each platform.
To interact with the Facebook SDK, there is a react-native-fbsdk
library in React Native.
You can install it by running the command in the terminal:
yarn add react-native-fbsdk
or
npm install --save react-native-fbsdk
Instructions for further connection can be found in the documentation for the library. BUT:
!!DO NOT USE react-native link react-native-fbsdk
!!
I am an adherent of manual linking libraries in React Native. So I do not recommend using the react-native link
. Since using link scripts, I can never be 100% sure that my project will start and these scripts will not break anything. Moreover, manual linking is a simple operation and is usually well described in the documentation for libraries using native modules.
For example, for iOS, there is a description of the manual linking process for all libraries in the general case in React Native official documentation.
2.1. [Android] Connecting Facebook SDK and setting up Facebook APP ID
In the case of the react-native-fbsdk
library, the manual linking process is described in the configuration section. Following these instructions, you can easily link the library.
Setting up a Facebook APP ID
in Android application
This configuration is described in the Facebook documentation, and there are links to the necessary pages in react-native-fbsdk
. However, you do not need to do everything described here. Here is the "extract" from the React Native documentation.
From the official documentation, we need to perform only the Add Facebook App ID step:
In Android, we need to add Facebook APP ID
to the Android manifest. Perform the next:
- Open
/app/src/main/res/values/strings.xml
file - Add the following line:
- Open
/app/src/main/AndroidManifest.xml
- Add a line with
meta-data
to the elementapplication
:
2.2. [iOS] Connecting Facebook SDK and setting up Facebook APP ID
Unfortunately, there is no description of manual linking in the documentation of the react-native-fbsdk
library so that I will describe it here:
1. Download Facebook SDK
If you are setting up a Facebook SDK for iOS for the first time on your machine, make sure that you download the latest Facebook SDK from here (the ‘Download’ section) with the archive and unpack it into the ~/Documents/FacebookSDK
folder
It should contain something like this:
2. Link the native project
Add to PROJECT_NAME -> TARGETS -> PROJECT_NAME -> Build Settings -> Framework Search Paths
the path to the Facebook SDK:
$(HOME)/Documents/FacebookSDK/
Add frameworks: FBSDKCoreKit.framework
, FBSDKLoginKit.framework
and FBSDKShareKit.framework
to Link Binary With Libraries:
The most frequent problems
- Once again: if you are setting up a Facebook SDK for iOS for the first time on your machine, make sure that you download the latest Facebook SDK from here (the ‘Download’ section) with the archive and unpack it into the
~/Documents/FacebookSDK
folder. - Also, be sure to add the path
$(HOME)/Documents/FacebookSDK/
toPROJECT_NAME -> TARGETS -> PROJECT_NAME -> Build Settings -> Framework Search Paths
(see screenshot above). - If the error
FBSDKShareKit/FBSDKShareKit.h not found
appeared, and you have completed the previous paragraph, then this can be solved in two ways: 1 or 2. The second one was helpful to me. - If you are using
Xcode 10
and received an error:Cycle in dependencies between targets
, check this - Before making the build with the changes, do
Product -> Clean
If these solutions did not help you, look in the troubleshooting section of react-native-fbsdk
.
3. Register Facebook APP ID
in iOS application
From the official documentation, we need to perform only the steps Configure Info.plist and Connect App Delegate:
- Open
/ios/ProjectName/Info.plist
and add the lines:
- Replace {your-app-id}, and {your-app-name} with your app’s App’s ID and name found on the Facebook App Dashboard.
- Open
/ios/ProjectName/AppDelegate.m
and add the lines:
So, Facebook SDK configuration is completed, and you can start embedding various services in your application.
3. Authorization in the application through Facebook
To begin, I propose to consider the scheme of the authorization process in the application with own backend using Facebook authentication.
- A user initiates an action
- The application sends a request for authentication to Facebook
- The user enters his data and, in the case of successful authentication, Facebook returns Facebook Token to our application.
- Next, we try to log in to our application and send Facebook Token to the backend for a specific route.
- Backend verifies the Facebook Token we sent.
- Backend performs authorization (registration or login)
- Backend returns us AuthToken for our system.
Authorization setup through Facebook is well described in the official documentation for React Native.
3.1. Connecting Facebook Login product
I indicate this step here to remind you that we need to connect the Facebook Login product to the Facebook console.
3.2. Advanced settings for Android
To make Facebook Login working in your React Native application, you need to make several more settings.
1. Add lines to /app/src/main/res/values/strings.xml
:
2. And a couple of lines more in /app/src/main/AndroidManifest.xml
:
3. Also, you need to register Key Hashes in the platform settings in Facebook Settings. You need to register the keys for debugging as well as for release versions.
As written in the official Facebook documentation:
Facebook uses the key hash to authenticate interactions between your app and the Facebook app. If you run apps that use Facebook Login, you need to add your Android development key hash to your Facebook developer profile.
[MacOS]: To get Key Hash
for development builds, you need to run this command with the password " android ":
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
Read more about Key Hashes
here.
!!N.B.: to run applications on Android, you also need to register Key Hashes
for the debug and release versions
3.3. An example of getting a token through the Login Manager
File sessionUtils.js
File App.js
Result — Android
Result — iOS
Conclusions
This post described the detailed process of connecting the Facebook SDK and Facebook Login to your React Native application. In general, integration consists of the following components:
1. Facebook
1.1. creating an application
1.2. adding platforms
1.3. [Android] add key hashes
1.4. add product Facebook Login
2. React Native
2.1. installing the Facebook SDK to the application
2.2. adding Facebook APP ID
to Info.plist
and AndroidManifest.xml
2.3. configuring Android application (AndroidManifest.xml
)
2.4. using Facebook SDK features
The most painstaking work is to connect the Facebook SDK to the iOS application properly.
Also, I do not recommend using scripts for auto-linking libraries in React Native. Moreover, even for the library that is used to interact with the Facebook SDK, these scripts are not developed well.
The process of interaction with the Login Manager itself is described in several lines, that is good news :)
Good luck!
Useful links
Originally published at https://blog.webbylab.com.