Passport-Facebook not providing email even if it is in scope
Asked Answered
S

3

20

In my application i register the facebook-strategie as follows: But the returned profile does not contain the email-field....

passport.use(new FacebookStrategy({
        clientID: config.facebook.clientID,
        clientSecret: config.facebook.clientSecret,
        callbackURL: config.facebook.callbackURL,
        passReqToCallback: true
    },
    function(req, accessToken, refreshToken, profile, done) {
        // No email in the following colsole.log
        console.log(JSON.stringify(profile));
    }));

The get is as follows:

app.get('/oauth/facebook', passport.authenticate('facebook', {
    failureRedirect: '/login',
    scope:['email']
}));

(So i am using scope as said here: Passport-facebook doesn't get email)

On the FB-Login Page iam even asked for the email and i do provide it: enter image description here

Any help is very appriciated!

Schock answered 7/7, 2015 at 20:54 Comment(3)
You need to ask for the email field alsoKweiyang
How do i do this? I Thought i do this with scope:['email']Schock
scope:['email'] is which permissions you want. When you call /me you need to do /me?fields=emailKweiyang
S
70

From Facebook graph APIv2.4, we need to explicitly specify fields to get.

Introducing Graph API v2.4

So, we can write like:

  passport.use(new FacebookStrategy({
      clientID: config.facebook.clientID,
      clientSecret: config.facebook.clientSecret,
      callbackURL: config.facebook.callbackURL,
      profileFields: ['id', 'email', 'gender', 'link', 'locale', 'name', 'timezone', 'updated_time', 'verified'],
    },
Stile answered 22/7, 2015 at 5:18 Comment(1)
I'm surprised this is not mentioned in official passport.js docs.Fanaticize
N
2

You do have the callback part of the code, right?:

app.get('/oauth/facebook/callback', passport.authenticate('facebook', {
    failureRedirect: '/login',
    successRedirect: '/',
    scope:['email']
}));

And, yes, indeed, this should be done with the scope:['email'], as per instructions from your link and this one here also.

Nub answered 8/7, 2015 at 18:55 Comment(1)
Yes, i also do have the email on the callback... This is strangeSchock
H
1

You need to specify the scope: "email". Refer the below code.

Facebook authentication route:

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

and while configuring the FacebookStrategy, you need to also specify the profileFields.

passport.use(new FacebookStrategy({
  callbackURL: "http://localhost:5000/auth/facebook/redirect",
  clientID: keys.facebook.clientID,
  clientSecret: keys.facebook.clientSecret,
  profileFields: ['id', 'displayName', 'photos', 'email', 'gender', 'name']
}, (accessToken, refreshToken, profile, done) => {
   // logic 
}))
Henpeck answered 11/6, 2019 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.