ionic-firebase-facebook-login
Firebase Facebook Authentication using Ionic
ionic-firebase-facebook-login

This article, we’ll learn how to implement firebase facebook authentication using Ionic. Before we starting we need to create facebook app id, app name for adding plugin. please refer https://codesundar.com/create-facebook-application/ for detailed instructions

Let’s create new project & Add required dependencies

ionic start firebasefbauth blank
cd firebasefbauth

ionic cordova platform add android
ionic cordova platform add ios

ionic cordova plugin add cordova-plugin-facebook4 --variable APP_ID="123456789" --variable APP_NAME="myApplication"
npm install --save @ionic-native/facebook

npm install --save firebase

Working with app.component.ts & app.module.ts

After adding all plugins and packages, we need to initiate our firebase inside app.component.ts


firebase.initializeApp({
      apiKey: "xxx",
      authDomain: "xxx",
      databaseURL: "xxx",
      projectId: "xxx",
      storageBucket: "xxx",
      messagingSenderId: "xxx"
    });

We need to declare plugin inside app.module.ts


import { GooglePlus } from '@ionic-native/google-plus';
import { Facebook } from '@ionic-native/facebook';
.
.
.
.
providers: [
    StatusBar,
    SplashScreen,
    Facebook,
    GooglePlus,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]

Working with home.html

Let’s create a new button for login with facebook. when the user click that button, we need to take two decision.

if app is running on browser, we need to call this code for a signup purpose


   if (this.platform.is('core')) {
      firebase.auth().signInWithPopup(new firebase.auth.FacebookAuthProvider()).then(fbRes => {
        this.displayToast('login success');
        this.userData = fbRes.additionalUserInfo.profile;
      }).catch(err => this.displayToast(err));
    }

if app is running on device, we can login with cordova plugin, here we also calling Facebook api (optional) for retrieving user’s information from Facebook such as email, first name, profile picture and gender


else {
      this.facebook.login(['public_profile', 'email']).then(res => {
        let credential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
        firebase.auth().signInWithCredential(credential).then(() => {
            // fetching user data
            this.facebook.api("me/?fields=id,email,first_name,picture,gender", ["public_profile", "email"]).then(data => {
              console.log(data)
            }).catch(err => this.displayToast(err));

          }).catch((err) => this.displayToast(err));

      }).catch(err => this.displayToast(err));
    }

 

!! important !!

If you’re using cordova-android@7.x.x, you need add these lines with your config.xml to avoid building errors

<platform name="android">
<config-file parent="/resources" target="./res/values/strings.xml">
<string name="fb_app_id">Your App ID</string>
<string name="fb_app_name">Your app name</string>
</config-file>
Sundaravel M

About the author: Hey! It's me sundar. (Yea, I'm the founder of codesundar.com). I'm a passionate computer programmer who loves modern web and mobile development. feel free to reach me : Facebook - Twitter - Google+