Don't receive email from Facebook OAuth API Passport with MEAN stack
I am working on creating a Face Book OAuth for a user to login to my site. I am getting an object back from Facebook with FB information, but there are no emails in this profile response.
When I click the "Sign in with Facebook" button on my page, it takes me to Facebook and says that the app will need my profile and email address as I added an email to my stuff. Here is my code:
Config / routes.js:
app.get('/auth/facebook', passport.authenticate('facebook', {scope:['email']}));
app.get('/auth/facebook/callback',
passport.authenticate('facebook',
{ successRedirect: '/confirm_community',
failureRedirect: '/signin' }));
============== passport.js
passport.use(new FacebookStrategy({
// pull in our app id and secret from our auth.js file
clientID : configAuth.facebookAuth.clientID,
clientSecret : configAuth.facebookAuth.clientSecret,
callbackURL : configAuth.facebookAuth.callbackURL
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
console.log(profile);
// console.log(profile.emails[0].value);
User.findOne({'facebook.id':profile.id}, function (error, result){
if(error){
return done(error);
}
if(user) {
return done(null, user);
}
else {
var newUser = new User();
newUser.facebook.id = profile.id;
newUser.facebook.token = accessToken;
newUser.facebook.name = profile.displayName;
newUser.facebook.email = profile.emails[0].value;
newUser.save(function (error){
if(error){
console.log(error);
throw error;
} else {
return done(null, newUser);
}
});
}
});
});
}));
Here is the FB object that I return when console.og (profile) in process.nextTick function with FB response:
{ id: 'my id is coming back here',
username: undefined, (because I don't have one)
displayName: 'my name is coming back here',
name:
{ familyName: undefined, (I don't have any of these in my profile)
givenName: undefined,
middleName: undefined },
gender: undefined,
profileUrl: undefined,
provider: 'facebook',
_raw: '{"name":"my name is coming here","id":"my id is coming here"}',
_json: { name: 'my name is coming here', id: 'my id is coming here' } }
I have made sure that my email is public and usually logs into Facebook with my email and password, so I do not use my mobile phone to log into Facebook. I just missed this email address. I can't figure out why this object isn't returning an email to me? What am I missing? Help is greatly appreciated.
source to share
I found another solution for this problem with the PASSPORT-FACEBOOK node module. Hope this helps others.
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:3000/auth/facebook/callback",
profileFields: ['email']
Link: https://github.com/jaredhanson/passport-facebook/issues/129
source to share
Find "declarative fields" in the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4
Now you need to specify the fields you want to receive in the API call: /me?fields=name,email
source to share