Facebook Login
Integrate Facebook login in Ionic 5 apps
Steps to follow for adding facebook login

Step 1: Facebook Developer Console

The first thing we need to do is to create a new application in Facebook’s developer dashboard, and this app is the one that Facebook will use to ask our users for their permission when we try to log them into our Ionic application.
For that you’ll need to go to facebook developer console and create a new app.
create facebook app
Once we finish creating our app, we will go to facebook app dashboard and take app id from there. It will help us in installing Facebook plugin in our app

Step 2: Install the Cordova Plugin

In this step we will install the Cordova plugin in our Ionic app.
For that, open your terminal and type
ionic plugin add cordova-plugin-facebook4 --variable APP_ID="123456789" --variable APP_NAME="myApplication"
You’ll need to replace the values or APP_ID and APP_NAME for your real credentials. You can find both of those inside your Facebook Developers Dashboard.
It's bit clumsy to work with cordova plugin so ionic team created Ionic Native, which is a wrapper for the Cordova plugins so we can use them in a more “Angular/Ionic” way.
So now we will open our terminal and try this command
npm install --save @ionic-native/facebook
for installing facebook package from Ionic Native

Step 2: Add your Platforms to Facebook

Once everything is set up in our development environment, we need to let Facebook know which platforms we’ll be using (if it’s just web, iOS, or Android).
In our case, we will add all three platforms iOS , web and Android.
To add the platforms, go ahead and inside your Facebook dashboard click on settings, then, right below the app’s information you’ll see a button that says Add Platform, click it.
To add the platforms, go ahead and inside your Facebook dashboard click on settings, then, right below the app’s information you’ll see a button that says Add Platform, click it.
Once you click the button, you’ll see several options for the platforms you’re creating
Add Platforms in facebook dashboard
, let’s start with iOS, you’ll see a form asking you for some information, right now we just need the Bundle ID.
If you don’t know where to get the Bundle ID, it’s the same as the package name when you create an Ionic app, it’s inside your config.xml file:
<widget id="co.ionic.facebookAUth" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
Once you add the Bundle ID, just follow the process to create the app.
for Android, the difference is that instead of Bundle ID, Android calls it “Google Play Package Name.”

Step 3: Enable Facebook Sign-In in Firebase.

Now that everything is set up on Facebook’s side, we need to go into our Firebase console and enable Facebook authentication for our app.
To enable Facebook, you’ll need to go to your Firebase Console and locate the app you’re using.
Once you’re inside the app’s dashboard, you’re going to go into Authentication > Sign-In Method > Facebook and are going to click the Enable toggle.
It will ask you App ID and App secrete you will copy it from your Facebook console, under your app’s settings.

Step 4: Authenticate Users.

The first thing we need to do is to get the authorization token from Facebook, to do that, we need to import Facebook plugin from Ionic Native and ask our user to log in.
import { Facebook, FacebookLoginResponse } from '@ionic-native/facebook/ngx';
constructor(private fb : Facebook, private platform: Platform)
{
}
doLogin() {
if(this.platform.is('cordova')){
this.fb.login(['email'])
.then((response:FacebookLoginResponse) => {
this.loginWithFacebook(response.authResponse.accessToken);
}).catch((error) => {
alert('error:' + JSON.stringify(error))
});
} else{
this.fbLogin();
}
}
This function first we will check platform of user if it is browser then we will open pop up for facebook log in and if it is IOS or android platform then we follow native facebook login process which will give you Firebase Token and from this token we will login user to firebase app
loginWithFacebook(accessToken) {
this.loginType = 'Login with Facebook'
const credential = firebase.auth.FacebookAuthProvider
.credential(accessToken);
return this.fireAuth.auth.signInWithCredential(credential)
}
fbLogin(): Promise<any> {
this.loginType = 'Login with Facebook'
return this.fireAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider())
}
Copy link
On this page
Step 1: Facebook Developer Console
Step 2: Install the Cordova Plugin
Step 2: Add your Platforms to Facebook
Step 3: Enable Facebook Sign-In in Firebase.
Step 4: Authenticate Users.