Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Facebook Authentication

Emmanuel Nwogbo
PLUS
Emmanuel Nwogbo
Courses Plus Student 2,434 Points

OAuth Authentication With Passport course

I just finished the OAuth Authentication With Passport course, still trying to digest a few concepts. Everything works fine, until I actually try to login to my test app with Facebook or github. Instead of being routed to the profile page, I get this error:

Cannot read property '0' of undefined

TypeError: Cannot read property '0' of undefined
    at Strategy.generateOrFindUser [as _verify] (C:\projects\Side         Projects\passyapp\app.js:16:19)
    at C:\projects\Side Projects\passyapp\node_modules\passport-  oauth2\lib\strategy.js:193:24
    at C:\projects\Side Projects\passyapp\node_modules\passport-  github\lib\strategy.js:174:7
    at passBackControl (C:\projects\Side Projects\passyapp\node_modules\oauth\lib\oauth2.js:134:9)
    at IncomingMessage.<anonymous> (C:\projects\Side Projects\passyapp\node_modules\oauth\lib\oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

BELOW IS A SNIPPET FROM MY APP.JS FILE:

function generateOrFindUser(accessToken, refreshToken, profile, done){
  if(profile.emails[0]) {
    User.findOneAndUpdate({
      email: profile.emails[0]},
    {
      name: profile.displayName || profile.username,
      email: profile.emails[0].value,
      photo: profile.photos[0].value
    },
    {
      upsert: true
    },
    done);
  }
  else {
    let noEmailError = new Error("Your email privacy setting prevent you from signing in");
    done(noEmailError, null);
  }
}

//configure github startegy
passport.use(new GitHubStrategy({
  clientID: process.env.GITHUB_CLIENT_ID,
  clientSecret: process.env.GITHUB_CLIENT_SECRET,
  callbackURL: "http://localhost:3000/auth/github/return",
  profileFields   : ['id', 'email']
}, generateOrFindUser));

//Configure facebook strategy
passport.use(new FacebookStrategy({
    clientID: process.env.FACEBOOK_APP_ID,
    clientSecret: process.env.FACEBOOK_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/facebook/return",
    profileFields   : ['id', 'email']
  }, generateOrFindUser));

I've been stuck the whole day; any help would be nice.

1 Answer

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Extremely late response, but for anybody else reading, you don't need a profileFields for the github strategy. And what worked for me was adding a scope to my github strategy like this:

scope: 'user:email'

I know Andrew didn't use this, but maybe this requirement was added later.