Passport-Facebook authentication is not providing email for all Facebook accounts
Make sure these two things are in your code:
passport.use(new FacebookStrategy({
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackURL: "http://www.example.com/auth/facebook/callback"
passReqToCallback : true,
profileFields: ['id', 'emails', 'name'] //This
},
and this:
app.get('/connect/facebook', passport.authorize('facebook', { scope : ['email'] }));
This gives you access to the following:
- profile.id
- profile.name.givenName
- profile.name.familyName
- profile.emails
The last one being an array, so use profile.emails[0].value
to get the first email address of the user.
As shamim reza
pointed out, you might want to check if profile.emails !== undefined
because the property only exists if the user has at least one verified email address.
As Weft
pointed out, you might have to use the property email
instead of emails
.
When you are authenticating use something similar to this. You need to use add 'email' in the scope when you authenticate.
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['email']}),
function(req, res){
});
That has worked for me.
Here were a few links that helped me out.
https://github.com/jaredhanson/passport-facebook/issues/11 https://github.com/jaredhanson/passport-facebook#how-do-i-ask-a-user-for-additional-permissions