Emily
Emily

Reputation: 1151

How to send Facebook authentication details to Firebase using a Cordova plugin & Firebase template

Sorry this question is kind of long, it's because I've been trying to solve this problem for a while and want to make sure I don't leave any info out. I'm building a Cordova app and using Firebase for the authentication/database back end. I've been trying to authenticate users into Firebase using a Log in with Facebook button for almost a week now, but I haven't been able to get it to work.

Originally I tried following Firebase's example here: https://firebase.google.com/docs/auth/web/facebook-login (I need to use the "Advanced: Handle the sign in flow manually" as it is a Cordova Android & iOS app), this example didn't work for me as the link to Facebook's SDK script (//connect.facebook.net/en_US/sdk.js) kept throwing the error:

file://connect.facebook.net/en_US/sdk.js Failed to load resource: net::ERR_FILE_NOT_FOUND

I tried to fix this error in several ways, such as:

None of these attempts worked. So instead I decided to use the plugin cordova-plugin-facebook from here: https://github.com/bisrael/cordova-plugin-facebook

This is the code I'm using to get the user's information from Facebook with the plugin:

function logInWithFacebook(){
    CordovaFacebook.login({
    onSuccess: function(result) {
         console.log(result);
         console.log(result.authToken);
         // Store or send the user auth/access key here?
         // Get user's name
         retrieveUserDetails(); 
    if(result.declined.length > 0) {
         alert("The User declined something!");
      }
   },
   onFailure: function(result) {
      if(result.cancelled) {
         alert("The user doesn't like my app");
      } else if(result.error) {
         alert("There was an error:" + result.errorLocalized);
      }
   }
});
}

function retrieveUserDetails(){
    // Now that the user has authroised the app, make request to CordovaFacebook plugin to get user's name
    CordovaFacebook.graphRequest({
        path: '/me',
        params: { fields: 'name' },
        onSuccess: function (userData) {
            console.log(userData);
            console.log(userData.name);
        // Here somehow send the retrieved username and send it to the Firebase function so that it's linked with the auth key.
        },
        onFailure: function (result) {
            if (result.error) {
                Error.log('error', 'There was an error in graph request:' + result.errorLocalized);
            }
        }
    });
}

I'm now able to click on a log in button and log in successfully through Facebook. That process is returning a user auth/access key and the user's name from Facebook.

As I understand it, the manual log in flow example in Firebase's docs (https://firebase.google.com/docs/auth/web/facebook-login) takes the key returned from Facebook, converts it into a Firebase key, and then enters the user's newly created Firebase key and their username into Firebase's servers.

This seems pretty straight forward in the following sample code:

function checkLoginState(event) {
  if (event.authResponse) {
    // User is signed-in Facebook.
    var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) {
      unsubscribe();
      // Check if we are already signed-in Firebase with the correct user.
      if (!isUserEqual(event.authResponse, firebaseUser)) {
        // Build Firebase credential with the Facebook auth token.
        var credential = firebase.auth.FacebookAuthProvider.credential(
            event.authResponse.accessToken);
        // Sign in with the credential from the Facebook user.
        firebase.auth().signInWithCredential(credential).catch(function(error) {
          // Handle Errors here.
          var errorCode = error.code;
          var errorMessage = error.message;
          // The email of the user's account used.
          var email = error.email;
          // The firebase.auth.AuthCredential type that was used.
          var credential = error.credential;
          // ...
        });
      } else {
        // User is already signed-in Firebase with the correct user.
      }
    });
  } else {
    // User is signed-out of Facebook.
    firebase.auth().signOut();
  }
}

function isUserEqual(facebookAuthResponse, firebaseUser) {
  if (firebaseUser) {
    var providerData = firebaseUser.providerData;
    for (var i = 0; i < providerData.length; i++) {
      if (providerData[i].providerId === firebase.auth.FacebookAuthProvider.PROVIDER_ID &&
          providerData[i].uid === facebookAuthResponse.userID) {
        // We don't need to re-auth the Firebase connection.
        return true;
      }
    }
  }
  return false;
}

FB.Event.subscribe('auth.authResponseChange', checkLoginState);

My question is, how can I send the auth key and username returned from the Cordova plugin code, to Firebase's example code so that it works smoothly?

Firebase's example code includes this listener which listens for any change in the Facebook authorization status: FB.Event.subscribe('auth.authResponseChange', checkLoginState); but as this uses Facebook's SDK it won't work with my current set up.

I'm using the following Firebase chat app as a template to work from: https://gist.github.com/puf/8f67d3376d80ed2d02670d20bfc4ec7d as you can see it has a Login with Facebook button, but no code for handling the process, I'm trying to apply parts of the manual log in flow example in Firebase's docs (https://firebase.google.com/docs/auth/web/facebook-login) with data returned from the cordova-plugin-facebook queries, and integrate both with Firebase's chat app template.

I'm really at a loss as to what to do next, I've tried everything I can think of. Any help in solving this problem would be really, really appreciated. Thank you in advance!

UPDATE

Questions and answers:

How does it work at the moment?

Right now I have a "Facebook Login" button - when this is clicked it runs logInWithFacebook(). This function uses the CordovaFacebook plugin, it also runs the function retrieveUserDetails() after the user signs in with Facebook. retrieveUserDetails() gets some user info from Facebook which I hope to then insert into my Firebase database.

logInWithFacebook() works correctly (it opens up a Facebook login page, and when the user logs in, I'm able to console.log the user's Facebook ID, and the Facebook access Token.

retrieveUserDetails() also works correctly (I'm able to console.log the user's name taken from Facebook).

How do you want it to work?

I'm happy with how the first half of the process is working (the logging in with Facebook and retrieving user details is working correctly). However I want this log in to trigger Firebase's auth state change listener, so that Firebase detects and confirms that the user has logged in:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    console.log("User is signed in.");
  } else {
    console.log("User is not signed in.");
  }
});

What is not working the way you want?

The first half of the process is working correctly, but I'm lost when it comes to what to do with the accessToken returned from Facebook. From reading the docs I think that Firebase is supposed to convert this token into a Firebase access token, and then that is used to log the user into Firebase (this would also trigger the above AuthStateChanged function). From there I want to be able to insert any data I've retrieved from Facebook (the user's name etc) into my Firebase database. But the main problem is getting the Facebook accessToken converted into a Firebase login (the second block of code in my original question is where I'm trying to perform the conversion/sign into Firebase).

Because I'm using Cordova, this method (logging into Facebook with a plugin and then handling the conversion of the accessToken) seems to be the only way to log in with Facebook. But I'm totally lost on how to complete the second half.

UPDATE 2

I've trimmed parts from the sample convert-Facebook-token-to-Firebase-token code from the docs so that the Facebook SDK isn't required. And it appears to be working. This is the code after cutting away the SDK related parts:

// First, define the Facebook accessToken:
var FBaccessToken = result.accessToken;

// Build Firebase credential with the Facebook auth token.
var credential = firebase.auth.FacebookAuthProvider.credential(
    FBaccessToken);
// Sign in with the credential from the Facebook user.
firebase.auth().signInWithCredential(credential).then(function(user){

console.log("It looks like we signed into Firebase with the Facebook token correctly.");

}, function(error) {
    console.log("Something went wrong, user isn't signed into Firebase with the FB token.");
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
});

I still need to add the user's email from Facebook and try to send that while logging into Firebase too - so that I'll have some identifier for the user in the Firebase console, but this is a good start.

2nd UPDATE

The below code successfully gets user data from Facebook after the user authorizes the app:

    CordovaFacebook.graphRequest({
    path: '/me',
    params: { fields: 'first_name,last_name,email,locale,gender,age_range,picture.width(200).height(200)' },
    onSuccess: function (userData) {
        console.log(userData)
        var first_name = userData.first_name;
        var last_name = userData.last_name;
        var email = userData.email;
        var locale = userData.locale;
        var gender = userData.gender;
        var min_age = userData.age_range.min;
        var profile_picture = userData.picture.data.url;

            // Enter user details into the Firebase database:
            firebase.database().ref('users/' + uid).set({
            first_name: first_name,
            last_name: last_name,
            email: email,
            locale: locale,
            gender: gender,
            min_age: min_age,
            profile_picture : profile_picture
        });
        console.log("Facebook user data should now be in the database!");   
    },
    onFailure: function (result) {
        if (result.error) {
            Error.log('error', 'There was an error in graph request:' + result.errorLocalized);
        }
    }
});

Upvotes: 2

Views: 1606

Answers (1)

ArneHugo
ArneHugo

Reputation: 6509

(Just an answer to the last update, as you figured out the rest :))

How to get user email from CordovaFacebook.login()

Looking at the CordovaFacebook documentation you can add a permissions property on the object passed to the login method.

According to the Facebook API documentation the permission name for email is just "email".

I haven't tested, but I think this should work:

CordovaFacebook.login({
    permissions: [ 'email' ],
    onSuccess: function(result) {
        console.log('email:', result.email);
        ...
    },
    onFailure: function(result) {
        ...
    }
});

Upvotes: 1

Related Questions