Search code examples
javascriptfacebookgoogle-plusfirebasesimplelogin

Get email with Facebook Firebase Simple Login



I am using Facebook with Firebase Simple Login. I am trying to get the person's real name, which I have successfully done via user.displayName in my FirebaseSimpleLogin instance. When I try getting their email with user.email, it returns undefined. I have done the same thing with Google+ and it works just fine, no undefineds returned. Here is my code:

var chatRef = new Firebase('https://******.firebaseio.com');
//No, i didn't actually put this firebase name in my code =P
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
  if (error) {
    // an error occurred while attempting login
    console.log(error);
    alert('An error occurred while trying to log you in.' + error);
  } else if (user) {
    // user authenticated with Firebase
    console.log('Name: ' + user.displayName + ', email: ' + user.email);
    //With google, 'Name: John Doe, email: [email protected]' is returned
    //With facebook, 'Name: John Doe, email: undefined' is returned
  } else {
    // user is logged out
  }


});

function google() {
  auth.login('google', {
    rememberMe: true
  });
}

function facebook() {
  auth.login('facebook', {
    rememberMe: true,
    scope: 'email'
  });
}

So, my question is, how can i get the user's email address with Facebook Firebase Simple Login? Thank you all in advanced.


Solution

  • Before you start, the best way to see what properties already exist on the user object is to add a console log to your existing auth check.

    Using:

    else if (user){
      console.log(user);
    }
    

    you'll know what properties exist on the user object as you have it set up currently. Additional permission might be required - see below.

    Per facebook dev docs, permissions are strings that are passed along with a login request or an API call. Here are two examples of permissions:

    • email: Access to a person's primary email address.
    • user_likes: Access to the list of things a person likes.

    For example, if you add the login button to a web app and ask for email and user_likes via the scope parameter, a person would be prompted with this dialog when logging in for the first time:

    And per firebase Docs

    Here is an example where the session will be stored for 30 days (due to the 'remember me' setting), and we also request some extended permissions for email and user_likes:

    auth.login('facebook', {
      rememberMe: true,
      scope: 'email,user_likes'
    });
    

    So, unless you have added the additional permissions in the auth.login parameters, you will not be able to access the user's primary email address.