2

I am making an app in which I want the users to login through facebook. My code looks like the following:

<!DOCTYPE html>
<html>
<head>

<title>Myapp</title>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.18.min.js"></script>

</head>

<body>

<script>
function fblogin(){
Parse.initialize("APP_ID", "JS_KEY");
(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));



      window.fbAsyncInit = function() {
  Parse.FacebookUtils.init({
    appId      : 'xxxxxxxx', // Facebook App ID
    channelUrl : 'http://myapp.parseapp.com', // Channel File
    cookie     : true, // enable cookies to allow Parse to access the session
    xfbml      : true  // parse XFBML
  });

  Parse.FacebookUtils.logIn('email,user_friends', {
  success: function(user) {
    if (!user.existed()) {
      alert("User signed up and logged in through Facebook!");
    currentUser.set("email",email);
    currentUser.save(null,{
    success: function(){  alert('mail saved');},
    error: function(){ alert('mail saving failed');}
    });
    } else {
      alert("User logged in through Facebook!");
    }
  },
  error: function(user, error) {
    alert("User cancelled the Facebook login or did not fully authorize.");
  }
});
};


}

    </script>


<h1>NEW</h1>

<input type="submit" value="Login with facebook" onclick="fblogin();"/>


</body>

</html>

But what this code does is that it creates a user in the Data Browser of my Parse app but leaves the "email" field as "undefined". I tried to explicitly save the email in the above code but to no success. I seriously need the email to communicate with my users and it should be present in the class "User". Is there any way of achieving this?

Akshay Arora
  • 1,953
  • 1
  • 14
  • 30

2 Answers2

1

You need to make an additional call to the Facebook API. Something along these lines:

Parse.FacebookUtils.logIn('email,user_friends', {
    success: function(user) {
        if (!user.existed()) {
            alert("User signed up and logged in through Facebook!");

            FB.api('/me?scope=email', function(me) {
                user.set("email", me.email);
                user.save();
            });
        } else {
            alert("User logged in through Facebook!");
        }
    },
    error: function(user, error) {
        alert("User cancelled the Facebook login or did not fully authorize.");
    }
});

Note that you can only get the user's email if the user allows this, which should be the case since you specify it in your login permissions.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

You need extra permissions to access email. Use "scope=email" in the get string of your call.

See: https://stackoverflow.com/a/3611781/1397632

Community
  • 1
  • 1
Tgeo
  • 153
  • 1
  • 9