Reputation: 1218
I'm working to set up Face book OAuth for user logging into my website. I am getting an object back from Facebook with my FB information but there is no sign of email anywhere in that profile response.
When I click the "Login to Facebook" button on my page it takes me to Facebook and says the app will require my profile and email as I added email to the scope of things I wanted. 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 I get back when I console.log(profile) in the process.nextTick function with FB's 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 made sure my email is public and login to Facebook normally with my email and password, so I'm not using Mobile Phone to sign to Facebook. I'm just missing that email address. I can't seem to figure out why that object isn't giving me back an email?? What am I missing? Help is very much appreciated.
Upvotes: 3
Views: 987
Reputation: 2155
I find another solution for this issue with PASSPORT-FACEBOOK node module. Hope this will help others.
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:3000/auth/facebook/callback",
profileFields: ['email']
Ref : https://github.com/jaredhanson/passport-facebook/issues/129
Upvotes: 3
Reputation: 74004
Search for "Declarative Fields" in the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4
You now have to specify the fields you want to get in the API call: /me?fields=name,email
Upvotes: 1