Facebook Login returns "Undefined" fields in the user profile and does not return an email address. MEANJs + Passport-facebook

I use the templates Meanjs.org and Facebook Registration returns me to the registration page. Below are the steps that I have taken so far.

1) Setting up the Facebook application URL site

http: // localhost: 3000 /

and callback URI OAuth

http: // localhost: 3000 / auth / facebook / callback

2) Placing APP_ID and APP_Secret as Client_ID and Client_Secret

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' ] }, 

3) The code is as follows

- Routes

 // Setting the facebook oauth routes app.route('/auth/facebook').get(passport.authenticate('facebook', { scope: ['email'] })); app.route('/auth/facebook/callback').get(users.oauthCallback('facebook')); 

- oauthCallback function,

  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); }; }; 

- Passport-Facebook Strategy

 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); } )); 

};

4) Debugging

An error in registering with the oauthCallback function returns the following:

1TypeError: cannot read property '0' from undefined

What Facebook returns as a profile in the Passport-Facebook module is as follows:

{id: 'Id_of_the_person', username: undefined , displayName: 'Full_name_of_person', name: {familyName: undefined , givenName: undefined , middleName: undefined }, gender: undefined , profileUrl: undefined , 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: undefined }}

Can someone be kind to guide me about getting the right Facebook user profile, including the user's email?

Thank you very much.

+6
source share
2 answers

Firstly, the profileFields field is not subject to the Portable Contacts Agreement - and you can find the agreement for passport documents here .

Secondly, in your example, after deleting the deleted 'about_me', registering Facebook does not return an error. Before deleting 'about_me', I had another error: I tried accessing a nonexistent field (about_me) in node type (User)

If the error persists, see this series of 5 tutorials that helps me when I create a sign-in page for authentication with social network accounts.

0
source

I have profile fields that are set below:

 profileFields: ['email','id', 'first_name', 'gender', 'last_name', 'picture'] 

Even if you specify an email address, it can return emails if the user has several emails. So you need to check if email has been sent. Profile.email or profile.emails [0] .value. You should also check if it is undefined, because there are people who sign up for facebook who never check their email account, and there are people who sign up with a phone number, and in both cases their emails will always be undefined .

you want to verify that any required fields are relevant.

 var email = profile.email || profile.emails[0].value; if (! email){ console.log('this user has no email in his FB'); var err = {message:'this user is missing an email'}; return done(err); } 

now i can do it if they have email

 newUser.facebook.email = email; 

If they do not have an email, you can set up a session for the profile and send them to a page that asks them to enter an email.

It sounds like pain, but you can never trust information from a third-party api to have value.

Most examples of passports that I have seen on the Internet are wrong. All of them assume that there is an email.

0
source

All Articles