0

I used this code. I filled out everything, ID, Secret, callback url, and local database.

I set the urls on my facebook app to "http://localhost:3000/" and for the url of the website I tried both "http://localhost:3000/" and "http://localhost:3000/auth/facebook/callback"

Also I changed the port on the app from 8080 to 3000 to match it.

Whenever I clicked the login, the server would crash, telling me there was an uncaught error somewhere in a node module of mongoose and that it couldn't "read property 0 of undefined", so I figured that would happen if there was no permission to access the email scope (since it does newUser.facebook.email = profile.emails[0].value;), so I tried to comment out that line.

After commenting out that line, the server wouldn't crash anymore when doing the login, and when I was redirected to the profile page, I got the token, ID, and displayName.

So, the login works if I don't request the email, but when I add that line of code to request the email, it crashes.

I'm requesting the "email" scope like this:

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

I searched previous questions, but I couldn't find what I was looking for. What can I do?

Metsuryu
  • 35
  • 1
  • 8

2 Answers2

1

read property 0 of undefined for profile.emails[0].value means that profile does not have a property called emails.

I'm assuming, since it is a single profile, it is email and not emails. Furthermore, it could be a single email, and not an array of emails.

You can print the profile object and check what properties it has. Then use the right property that you need.

devxeq
  • 1,329
  • 2
  • 8
  • 15
  • OK, I tried that, and this is what is in "profile": id: the id username: undefined displayName: the name name: an object with familyName, middleName and givenName, all undefined. gender: undefined profileUrl: undefined provider: facebook _raw and _json: objects with displayName and id. So, I'm not getting any email at all. I guess it's because I'm not requesting the email scope correctly? How can I fix that? – Metsuryu Feb 21 '17 at 02:22
  • 1
    I'm just shooting in the dark here. The facebook user you are trying to access needs to have given access permissions for your app. – devxeq Feb 21 '17 at 02:34
  • I'm accessing with my fb account, and I tried already checking the permissions (it says the app can see the email), I tried removing and re-accepting the permissions, and using a different account, still nothing. – Metsuryu Feb 21 '17 at 02:50
0

Fixed it.

I found it was answered here

Quoting:

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'],
},

For some reason this is not in the passport.js documentation I read.

Community
  • 1
  • 1
Metsuryu
  • 35
  • 1
  • 8