Passport-facebook doesn't get email [duplicate]
Asked Answered
T

3

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 :)

Trover answered 14/9, 2014 at 11:32 Comment(9)
have you tried logging the profile object to see what it contains?Gayn
i tried it now. Profile contains no emails...Trover
what happens if you don't use profileFields parameter?Gayn
i have more information. but no emailTrover
maybe the user's email hasn't been verified?Gayn
maybe this question will helpGayn
perfect, adding 'scope' work, sorry but I could not find any answers. thank you very muchTrover
Please upvote the answer in the other question if it helped you. We can then merge both questions as they seem to be duplicates.Gayn
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.Fabulist
F
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 :-)

Fabulist answered 24/10, 2014 at 18:2 Comment(3)
Same as this: #22881376Fabulist
Had same problem this helped: #20291857Elastin
Ask that one tester to remove this app from his Facebook account. and then try again. it will workQuadrat
O
1

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

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

Omegaomelet answered 17/12, 2015 at 10:1 Comment(2)
please write more neat answer that is understood by community.Heuser
Thanks for your kind suggestion. As I am a new to stackoverflow, I am not familiar to the way of answering. Next time I will try my best.Omegaomelet
I
0
  passport.use(new FacebookStrategy({
    clientID: config.facebook.clientID,
    clientSecret: config.facebook.clientSecret,
    callbackURL: config.facebook.callbackURL,
    passReqToCallback:true
  },
Intravasation answered 16/10, 2015 at 8:57 Comment(1)
a short explanation of the answer would be useful to the OPApplique

© 2022 - 2024 — McMap. All rights reserved.