Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email key is missing in the profile object in passportjs #129

Open
sarsarahman opened this issue Jul 23, 2015 · 10 comments
Open

Email key is missing in the profile object in passportjs #129

sarsarahman opened this issue Jul 23, 2015 · 10 comments

Comments

@sarsarahman
Copy link

Trying to use Facebook authentication using passportjs on my mean application. But Email key is missing in the profile object

I am getting the following object

{
    id: '12345678901234567',
    username: undefined,
    displayName: 'Display Name',
    name: {
        familyName: undefined,
        givenName: undefined,
        middleName: undefined
    },
    gender: undefined,
    profileUrl: undefined,
    provider: 'facebook',
    _raw: '{"name":"Display Name","id":"12345678901234567"}',
    _json: {
        name: 'Display Name',
        id: '12345678901234567'
    }
}

here are the routes

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

    app.get('/auth/facebook/callback', passport.authenticate('facebook', {
        successRedirect: '/profile',
        failureRedirect: '/'
    }));

    app.get('/connect/facebook', passport.authorize('facebook', {
        scope: ['email']
    }));

    app.get('/connect/facebook/callback', passport.authorize('facebook', {
        successRedirect: '/profile',
        failureRedirect: '/'
    }));

here is the screenshot of my apps permission

Image of facebokapp

what could be going wrong?

@rohang07
Copy link

Try using the additional parameter profileFields while creating your strategy -

passport.use(new FacebookStrategy({
  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: "http://localhost:3000/auth/facebook/callback",
  profileFields: ['email']
    ....

This worked for me

@Vinayaktech
Copy link

@rohang07 : thanks for the solution, its also work for me.

@KanwalPrakashSingh
Copy link

@rohang07 this solution works the case for login , but when i try to register a user then issues appear which say bad gateway

@rohang07
Copy link

rohang07 commented Aug 7, 2015

@KanwalPrakashSingh facebook registration with similar code works for me.
Internal server errors can be due to improper handling of the data which facebook returns (among infinitely many others). Try to log at a few places in your facebook strategy and see where exactly it fails. Any error (as simple and elusive as a null) there can cause such issues.

jfresco added a commit to DemocracyOS/democracyos that referenced this issue Aug 11, 2015
@ttquang1063750
Copy link

@rohang07
profileFields: ['email'] is the good answer
Thanks. It work for me.

@lesterzone
Copy link
Contributor

..while creating your strategy 😄 then everything works as expected 😄

@ghost
Copy link

ghost commented Apr 22, 2016

Now I'm getting the exact same issue. Even after trying the code that supposedly works. Perhaps it's because Facebook's oAuth is v2.6?

@ghost
Copy link

ghost commented Apr 22, 2016

Yeah, it is. That code works fine with API v2.5 but not 2.6

@kevin-madhu
Copy link

kevin-madhu commented May 2, 2016

It works in API v2.6 for me.
passport.use(new Strategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: 'http://localhost:3000/login/facebook/return',
profileFields: ['id', 'email', 'gender', 'link', 'locale', 'name', 'timezone', 'updated_time', 'verified'],
enableProof: true
}, ...

See https://developers.facebook.com/docs/graph-api/securing-requests for details.

@nyn0x
Copy link

nyn0x commented Aug 5, 2020

the solutions above didn't work for me, but with adding scope:["email"] it works 😁😁

{
    clientID: process.env.FACEBOOK_APP_ID,
    clientSecret: process.env.FACEBOOK_APP_SECRET,
    callbackURL: "http://localhost:3000/api/auth/facebook/callback",
    profileFields: ["email"],
    scope: ["email"],
    enableProof: true,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants