How to use facebook login in react native using firebase

firebase facebook login api directly doesn’t work in react native because it’s built to be used with http or https protocol. So, in this article I will show you how to use facebook login in react native using firebase.

First we need to install two modules in are react native app ie react-native-fbsdk module and firebase module.

To install react-native-fbsdk module follow the steps given here :

Next, install firebase module using the command below :

Now go to and login using your google username and password. Click on ‘GO TO CONSOLE’ and then click on ‘Add Project’. Give name to your project and click on ‘Create Project’. Now click on ‘Add Firebase to your web app’, you will get a modal with firebase api details . For our app we need ‘apiKey’ and ‘databaseURL’.

One more module we will be using in this article, that is, reactnavigation. I have discussed this module in detail my previous articles. Make sure to go though that if you have not used reactnavigation module before :

Now we will add the code below in our index file :

In the above code, we have initialized firebase with ‘apiKey’ and ‘databaseURL’ we got from our firebase app. Next, we configured the reactnavigation with Login screen and Home screen so that we can navigate from login screen to home screen once the facebook authentication is successful.

Now, we will add code below to our login.js file :

In the above code first we are setting the showSpinner state to true, so that, when authentication state is being checked user can see spinner instead of blank screen.  When Login component mounts,  we check the the authentication state of the the user with firebase api. If authentication state is true ie the user is already authenticated then we simply redirect the user to Home Page with users info ( that is saved in firebase db when user was authenticated ).

If user login is for the first time then firebase auth state will be false. So, showSpinner will be set to false and instead of spinner user will se Facebook login button. When user press login button showSpinner state is set to true and user get to see spinner on screen. Then, Facebook authenticates user and generate a authentication token. We use this token to sign in user to firebase using firebase signInWithCredential method. After that, we save users data in firebase db. Mean while authentication state changes to true and user is redirected to home screen with users info, that is fetched from firebase db.

Here is a demo of Facebook login in react native with firebase