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.