passport-facebook
passport-facebook copied to clipboard
User email is NOT returned from facebook signup
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!
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.
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('/'); } );
:+1: confirmed that this worked for me
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?
Have some problem, if user have many email.
@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
@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.
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'] },
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.
@rafipiccolo solution worked. It seems like they need to update the api call from 2.4 to 2.9
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'] }) )
I tested with my own account and only got name and profile picture. Email is returned when I specified it in the scope.
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.
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']}
));
This profileFields: ['id', 'displayName', 'photos', 'email'] worked for me
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 👎
same issue in 2021, any help?
Have the same issue, scope and postFields didn't work, Will remove the shit FB login from the website, that's waste of time. 😡
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
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