14

I have already implemented the Facebook-LogIn in my website with express js and passport-Facebook. It works well (I get field profile), but the problem is that I don't get the Email. I get an error:

email   : profile.emails[0].value,

TypeError: Cannot read property '0' of undefined

My code:

passport.use('facebook',new FacebookStrategy({
        clientID            : config.facebook.id,
        clientSecret    : config.facebook.secret,
        callbackURL  : '/auth/facebook/callback',
        profileFields : ['id', 'displayName', 'emails','photos']
    }, function(accessToken, refreshToken, profile, done) {

        User.findOne({provider_id: profile.id}, function(err, user) {
            if(err) throw(err);
            if(!err && user!= null) return done(null, user);

            var user = new User({
                provider_id : profile.id,
                name                 : profile.displayName,
                email               : profile.emails[0].value,
                photo               : profile.photos[0].value,
            });
            user.save(function(err) {
                if(err) throw err;
                return done(null, user);
            });
        });
    }));

It would be great if someone could help me with the solution to my problem :)

mhatch
  • 4,441
  • 6
  • 36
  • 62
jcabello
  • 287
  • 3
  • 8
  • have you tried logging the profile object to see what it contains? – soulcheck Sep 14 '14 at 12:09
  • i tried it now. Profile contains no emails... – jcabello Sep 14 '14 at 12:17
  • what happens if you don't use profileFields parameter? – soulcheck Sep 14 '14 at 12:24
  • i have more information. but no email – jcabello Sep 14 '14 at 12:31
  • maybe the user's email hasn't been verified? – soulcheck Sep 14 '14 at 12:33
  • 5
    maybe [this question](http://stackoverflow.com/questions/22880876/passport-facebook-authentication-is-not-providing-email-for-all-fbaccounts) will help – soulcheck Sep 14 '14 at 12:34
  • perfect, adding 'scope' work, sorry but I could not find any answers. thank you very much – jcabello Sep 14 '14 at 12:44
  • Please upvote the answer in the other question if it helped you. We can then merge both questions as they seem to be duplicates. – soulcheck Sep 14 '14 at 12:54
  • I am having the exact same problem. Facebook login works for all of our testers apart from one. For some reason, one profile does not return an email. Going to the users profile on Facebook shows that they have two emails registered: a facebook.com one and a gmail.com one, but they are not returned when trying to log in with PassportJS. Can't figure out why. – Alex York Oct 24 '14 at 17:48

3 Answers3

12

I had the same problem. We had 10 test users, all 10 had email addresses associated with their Facebook account. But for one of the 10 testers, Facebook did not return the 'email' JSON property in the profile response. I have no idea why, since it looked identical to other Facebook profiles that worked fine.

The fix was to change this line:

passport.authenticate('facebook')

To this:

passport.authenticate('facebook', { scope: [ 'email' ] })

I still can't explain why it worked for 9/10, but not for one. Either way, it's fixed now :-)

Alex York
  • 5,392
  • 3
  • 31
  • 27
  • 1
    Same as this: http://stackoverflow.com/questions/22880876/passport-facebook-authentication-is-not-providing-email-for-all-fbaccounts – Alex York Oct 24 '14 at 18:03
  • Had same problem this helped: http://stackoverflow.com/questions/20291357/passport-facebook-cant-get-about-me-and-email-profile-fields – kaxi1993 Dec 11 '15 at 19:44
  • Ask that one tester to remove this app from his Facebook account. and then try again. it will work – Arun Tyagi Sep 02 '16 at 05:13
1

Try this passport.authenticate('facebook', { scope: [ 'email' ] })

Also add a new field profileFields: [ 'email' , 'name' ] in the facebookStrategy

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Ajay
  • 23
  • 6
0
  passport.use(new FacebookStrategy({
    clientID: config.facebook.clientID,
    clientSecret: config.facebook.clientSecret,
    callbackURL: config.facebook.callbackURL,
    passReqToCallback:true
  },
Bikash Ramtel
  • 115
  • 1
  • 3
  • 12