skip to Main Content

For my application(Node.js) i’m using passport-facebook and everything works well except that i can’t get user email. I found a lot of issues on this subject but for some reason they not fix my problem. I would be really preciate for some help. So my configurations:

app.js

app.get('/auth/facebook', passport.authenticate('facebook', {scope: 'email'}));

// in scope i tried a different options like ['email'] or ['emails'] and so go on

app.get('/auth/facebook/callback',
  passport.authenticate('facebook', {
    successRedirect: '/',
    failureRedirect: '/login'
}));

passport.js:

passport.use('facebook', new FacebookStrategy({
  clientID        : secret.facebook.clientID,
  clientSecret    : secret.facebook.clientSecret,
  callbackURL     : secret.facebook.callback
},
  function(access_token, refreshToken, profile, done) {
    console.log(profile) // here i got all profile data except email
    process.nextTick(function() {
      User.findOne({ 'facebook.id' : profile.id }, function(err, user) {
        if (err)
          return done(err);
          if (user) {
            return done(null, user);
          } else {
            var newUser = new User();
            console.log(profile.email) // here i got undefined
            newUser.facebook.id = profile.id;
            newUser.facebook.token = access_token;
            newUser.facebook.firstName = profile.name.givenName;
            newUser.facebook.lastName = profile.name.familyName;
            newUser.facebook.email = profile.email;

            newUser.save((err) => {
              if (err)
                throw err;
              return done(null, newUser);
            });
         } 
      });
    });
}));

facebook-secret.js

module.exports = {
  facebook: {
    clientID: '****',
    clientSecret: '********',
    callback: '/auth/facebook/callback',
    profileFields: ['id', 'email']
  }
}

in UserSchema:

facebook: {
    id: String,
    token: String,
    email: String,
    name: String,
  }

facebok – settingsfacebook-settings

graph – api

graph-api

2

Answers


  1. Chosen as BEST ANSWER

    in passport.js :

    passport.use('facebook', new FacebookStrategy({
      clientID        : secret.facebook.clientID,
      clientSecret    : secret.facebook.clientSecret,
      callbackURL     : secret.facebook.callback
    },
    

    change to:

     passport.use('facebook', new FacebookStrategy({
      clientID        : secret.facebook.clientID,
      clientSecret    : secret.facebook.clientSecret,
      callbackURL     : secret.facebook.callback,
      profileFields   : secret.facebook.profileFields
    },
    

    My profileField : profileFields: ['id', 'displayName', 'email', 'first_name', 'middle_name', 'last_name']


  2. Try to pass the scopes in url as param like you used in graph API(?fields=id,email…).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search