passport-facebook icon indicating copy to clipboard operation
passport-facebook copied to clipboard

User email is NOT returned from facebook signup

Open josietam opened this issue 9 years ago • 20 comments
trafficstars

Hi,

We use nodejs with passport-facebook for user to signup account with fb account. Recently we found that the passport-facebook is not returning the email. Anyone has the idea?

We have already defined the fields

    passport.use('facebook', new FacebookStrategy({
        clientID: fb.clientID,
        clientSecret: fb.clientSecret,
        callbackURL: fb.callbackURL,
        profileFields: ['id', 'email', 'name']
     }, function(accessToken, refreshToken, profile, done) {

And also the scope passport.authenticate('facebook', { scope: ['email', 'public_profile'] })

Would appreciate your help. thanks!

josietam avatar Jun 28 '16 04:06 josietam

I just found out that since version 2.4 fb returns these fields only upon request. By default they return id and name, but NOT email. We need this modified in passport-facebook to inject in the /me?fields=id,name,email... etc via profileFields: ['id','email','name'] in opt so go ahead and inject those and it will be working back again.

FB Graph API states the email may NOT always be available if the user signed up with the Phone number instead.

bibanul avatar Jul 09 '16 18:07 bibanul

Facebook is shit, developpers are getting crazy for standard shit like this. it's my 10th install (5+ different scripting languages and libs to fb connect) and everytime i cry. hope it helps

here's my working data :

passport.use(new FacebookStrategy({ clientID: 'xxxxx', clientSecret: 'xxxxxxxx', callbackURL: "http://localhost:3000/auth/facebook/callback", profileFields: ['id', 'displayName', 'email', 'birthday', 'friends', 'first_name', 'last_name', 'middle_name', 'gender', 'link'] }, function(accessToken, refreshToken, profile, cb) { User.findOrCreate({ facebookId: profile.id }, function (err, user) { return cb(err, user); }); } )); app.get('/auth/facebook', passport.authenticate('facebook', { authType: 'rerequest', scope: ['user_friends', 'email', 'public_profile'], } )); app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login', }), function(req, res) { res.redirect('/'); } );

rafipiccolo avatar Jul 16 '16 21:07 rafipiccolo

:+1: confirmed that this worked for me

dman777 avatar Oct 18 '16 03:10 dman777

No email field here either. The only things I get back are name and id. That's all. Even with profileFields and scope declared. Any ideas?

chinciusan avatar Nov 28 '16 12:11 chinciusan

Have some problem, if user have many email.

semeleven avatar Nov 28 '16 12:11 semeleven

@chinciusan There might be two reasons: a) the user registrated with a phone number and he has no email in the profile

b) the emails object returns an array so you must specify which email you want, generally you use [0] as it is the first email in the array and you have no guarantees user has more than one

enricopolanski avatar Nov 28 '16 12:11 enricopolanski

@VulcanRav Thanks for the tips but I already checked them. I'm using my own account. I only have an email address and no phone number. But the result contains just name and id . The email field is not present at all.

chinciusan avatar Nov 28 '16 13:11 chinciusan

same problem here :'(

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

passport.use(new FacebookStrategy({ clientID: ///// clientSecret: //// callbackURL: "http://localhost:8000/auth/facebook/callback", profileFields: ['id', 'email', 'displayName', 'photos'] },

orielbachar avatar Jan 25 '17 16:01 orielbachar

Facebook will not return the email for those users who's email is pending for confirmation, when a user creates new account on facebook a confirmation email is sent to the user, but if user did't confirm his email, facebook will not return his email in user info even you added email permission in scope, until user confirms his email.

benjipott avatar Jan 26 '17 13:01 benjipott

@rafipiccolo solution worked. It seems like they need to update the api call from 2.4 to 2.9

ops-gaurav avatar Jun 17 '17 23:06 ops-gaurav

If anyone is having issues with the findOrCreate this worked for me: User.findOrCreate({ where: {facebookId}, defaults: {name, email, avatar} }) .then(([user]) => { return cb(null, user) }) .catch(cb)

And make sure to do the following with email, as mentioned above: app.get( '/auth/facebook', passport.authenticate('facebook', { scope: ['email'] }) )

kylebirns avatar Mar 09 '19 00:03 kylebirns

I tested with my own account and only got name and profile picture. Email is returned when I specified it in the scope.

tiendq avatar Jun 20 '19 15:06 tiendq

For anyone seeing this in the future, you have to do 2 things:

/**
 * @api {get} /v1/auth/facebook
 * @apiName FacebookOAuth
 * @apiGroup Auth
 */
router.get("/facebook", passport.authenticate("facebook", { scope: ["email"] }));

and

const facebookStrategyOptions: any = {
  clientID: FACEBOOK_CLIENT_ID,
  clientSecret: FACEBOOK_CLIENT_SECRET,
  callbackURL: FACEBOOK_REDIRECT_URI,
  profileFields: ["email"]
};

and it will (or may?) work.

bephrem1 avatar Aug 02 '19 04:08 bephrem1

passport.use(new FacebookStrategy({
    clientID: process.env.FB_APP_ID,
    clientSecret: process.env.FB_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/facebook/callback",
    profileFields: ["email"]
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ facebookId: profile.id, email: profile.emails[0].value }, function (err, user) {
      return cb(err, user);
    });
  }
));

and


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

yeged avatar Mar 07 '20 00:03 yeged

This profileFields: ['id', 'displayName', 'photos', 'email'] worked for me

s8sachin avatar Apr 20 '20 20:04 s8sachin

This is a pain in 2020

router.get('/auth/facebook', passport.authorize('facebook', {
    authType: 'rerequest', 
    scope : ['email', 'public_profile'] }));

and

passport.use(new FacebookStrategy({
    clientID: process.env.FACEBOOK_APP_ID,
    clientSecret: process.env.FACEBOOK_APP_SECRET,
    callbackURL: process.env.FACEBOOK_CALLBACK_URL,
    profileFields: ['id', 'email', 'displayName', 'photos']
  },
  function(facebookAccessToken, refreshToken, profile, done) {

email is empty 👎

DineshCodeFlow avatar Aug 28 '20 08:08 DineshCodeFlow

image same issue in 2021, any help?

David05500 avatar Jun 12 '21 10:06 David05500

Have the same issue, scope and postFields didn't work, Will remove the shit FB login from the website, that's waste of time. 😡

sha-hin avatar Oct 07 '21 12:10 sha-hin

Have the same issue, scope and postFields didn't work, Will remove the shit FB login from the website, that's waste of time. 😡

1year ago we did the same thing. Now we don't have facebook loging

DineshCodeFlow avatar Oct 07 '21 12:10 DineshCodeFlow

Please check your Facebook app/page permission list for those still struggling to get email information. By default, public_profile permission is given. you have to add email to the permission list to make it work

shaikhalamin avatar Oct 11 '23 01:10 shaikhalamin