2
votes

I want to get first name, last name, email when user login with facebook. My app is in Cordova. I am using cordova plugin for facebook authentication this one "https://github.com/Wizcorp/phonegap-facebook-plugin". I can login with Facebook but how can I get name, email from it. Thanks in advance.

2

2 Answers

8
votes

add facebook cordova plugin using below command See Details

cordova plugin add https://github.com/Wizcorp/phonegap-facebook-plugin --variable APP_ID="YOURAPPID" --variable APP_NAME="YOURAPPNAME" 

html code

 <div onclick="fblogin();"><img src="img/facebook.png" /></div>

Javascript code

<script>
            function fblogin(){

            facebookConnectPlugin.login( ["public_profile","email"],
                function (response) {

                    if(response.authResponse.userID!=''){
                        facebookConnectPlugin.api(response.authResponse.userID+"/?fields=id,email,first_name", ["public_profile"],
                        function (response) {
                            console.log('SUCCESS:'+response);
                            alert('first_name : '+response.first_name+',email:'+response.email+',id:'+response.id);
                        },
                        function (response) {
                            console.log('ERROR:'+response);
                        });
                    }    

                }, 
                function (response) {   
                        console.log('ERROR:'+response);
               });

               }

               </script>
1
votes

Try this Login Function with below Snippet.

var fbLogin = function() {
    facebookConnectPlugin.login(["public_profile", "email", "user_birthday", "user_location"],
        function(response) {
            console.log("Login Response :" + JSON.stringify(response));
            //alert("Login Response :" + JSON.stringify(response))
            this.authId = response.authResponse.userID;
            if (response.status == "connected") {
                facebookConnectPlugin.api("/" + response.authResponse.userID, ["public_profile", "email", "user_birthday", "user_location"],
                    function(result) {
                        this.email = result.email;
                        this.firstname = result.first_name;
                        this.lastname = result.last_name;
                        this.birthdate = result.birthday;
                        this.city = result.location.name;
                    },
                    function(error) {
                        alert("Failed: " + error);
                    });
            }
        },
        function(response) {
            alert("Other Response : " + JSON.stringify(response))
        });
}