title: Facebook Login is returning 'Undefined' Fields in user profile and it doesn't return email. MEANJs + Passport-facebook
body: <p>I am using Meanjs.org boilerplate and Facebook Signup returns me to the Signup page.
  Following are the steps that I have taken so far.</p>
  
  <p><strong>1)</strong> Setting up the Facebook App <strong>Site URL</strong></p>
  
  <blockquote>
    <p><a href="http://localhost:3000/" rel="noreferrer">http://localhost:3000/</a></p>
  </blockquote>
  
  <p>and the callback <strong>URI of OAuth</strong></p>
  
  <blockquote>
    <p><a href="http://localhost:3000/auth/facebook/callback" rel="noreferrer">http://localhost:3000/auth/facebook/callback</a></p>
  </blockquote>
  
  <p><strong>2)</strong> Placing the APP_ID and APP_Secret in as Client_ID and Client_Secret</p>
  
  <pre><code>    facebook: {
      clientID: process.env.FACEBOOK_ID || '*****',
      clientSecret: process.env.FACEBOOK_SECRET || '*****',
      callbackURL: 'http://localhost:3000/auth/facebook/callback',
      profileFields: ['id','emails', 'first_name', 'last_name', 'displayName', 'link', 'about_me', 'photos' ]
  },
  </code></pre>
  
  <p><strong>3) Code is as follows</strong></p>
  
  <p>--Routes</p>
  
  <pre><code>// Setting the facebook oauth routes
  app.route('/auth/facebook').get(passport.authenticate('facebook', {
      scope: ['email']
  }));
  app.route('/auth/facebook/callback').get(users.oauthCallback('facebook'));
  </code></pre>
  
  <p>-- The oauthCallback function,</p>
  
  <pre><code>    exports.oauthCallback = function(strategy) {
      return function(req, res, next) {
          passport.authenticate(strategy, function(err, user, redirectURL) {
              if (err || !user) {
                  console.log('1' + err);
                  //console.log(user);
                  return res.redirect('/#!/signin');
              }
              req.login(user, function(err) {
                  if (err) {
                      console.log('2' + err);
                      return res.redirect('/#!/signin');
                  }
  
                  return res.redirect(redirectURL || '/');
              });
          })(req, res, next);
      };
  };
  </code></pre>
  
  <p>-- Passport-Facebook Strategy</p>
  
  <pre><code>module.exports = function() {
  // Use facebook strategy
  passport.use(new FacebookStrategy({
          clientID: config.facebook.clientID,
          clientSecret: config.facebook.clientSecret,
          callbackURL: config.facebook.callbackURL,
          passReqToCallback: true
      },
      function(req, accessToken, refreshToken, profile, done) {
  
          console.log('facebook Strategy Started');
          // Set the provider data and include tokens
          var providerData = profile._json;
          providerData.accessToken = accessToken;
          providerData.refreshToken = refreshToken;
  
      //  console.log(JSON.stringify(profile));
          console.log(profile);
  
      //  console.log(JSON.stringify(profile.name.givenName));
  
          // Create the user OAuth profile
          var providerUserProfile = {
              firstName: profile.name.givenName,
              lastName: profile.name.familyName,
              displayName: profile.displayName,
              email: profile.emails[0].value,
              username: profile.username,
              provider: 'facebook',
              providerIdentifierField: 'id',
              providerData: providerData
          };
  
          //console.log('provider' + providerUserProfile);
          // Save the user OAuth profile
          users.saveOAuthUserProfile(req, providerUserProfile, done);
      }
  ));
  </code></pre>
  
  <p>};</p>
  
  <p><strong>4) Debugging</strong></p>
  
  <p>Logging err under oauthCallback function returns the following,</p>
  
  <blockquote>
    <p>1TypeError: Cannot read property '0' of undefined</p>
  </blockquote>
  
  <p>What Facebook returns as profile in Passport-Facebook module is as follows,</p>
  
  <blockquote>
    <p>{ id: 'Id_of_the_person',
      username: <strong>undefined</strong>,
      displayName: 'Full_name_of_person',
      name:
       { familyName: <strong>undefined</strong>,
         givenName: <strong>undefined</strong>,
         middleName: <strong>undefined</strong> },
      gender: <strong>undefined</strong>,
      profileUrl: <strong>undefined</strong>,
      provider: 'facebook',
      _raw: '{"name":"Full_name_of_person","id":"Id_of_the_person"}',
      _json:
       { name: 'Id_of_the_person',
         id: 'Id_of_the_person',
         accessToken: 'access_token_value',
         refreshToken: <strong>undefined</strong> } }</p>
  </blockquote>
  
  <p>Can anyone be kind to guide me about getting the correct user profile from Facebook including user email?</p>
  
  <p>Thank you so much.</p>