Hermes Hermes - 15 days ago 7
Javascript Question

Cordova facebook plugin

I'm new on Cordova. I want to Facebook login on my app. I decised to use this Facebook plugin https://www.npmjs.com/package/cordova-plugin-facebook4. İf a user click the login button I want to get name, surname and email from the user. I didn't understand exactly how to use. I determined some functions but I dont not where I have to use. For example

Login

facebookConnectPlugin.login(Array strings of permissions, Function success, Function failure)


Logout

facebookConnectPlugin.logout(Function success, Function failure)


This is login js

var fbLoginSuccess = function (userData) {
console.log("UserInfo: ", userData);
}

facebookConnectPlugin.login(["public_profile"], fbLoginSuccess,
function loginError (error) {
console.error(error)
}
);

Answer

This should work to get the required data, after login:

facebookConnectPlugin.api("me/?fields=name,first_name,last_name,email", ["email"],
  function onSuccess (result) {
    console.log("Result: ", result);
  }, function onError (error) {
    console.error("Failed: ", error);
  }
);

Btw, you need to authorize the user with the correct permissions(s):

facebookConnectPlugin.login(["email"], fbLoginSuccess,
    function loginError (error) {
        console.error(error)
    }
);

How to use the login code on button click:

document.getElementById("login").addEventListener("click", function() {
    facebookConnectPlugin.login(["email"], fbLoginSuccess,
        function loginError (error) {
            console.error(error)
        }
    );
});
Comments