'use strict';

/**
 * Module dependencies.
 */
var passport = require('passport'),
    GithubStrategy = require('passport-github').Strategy,
    users = require('../../controllers/users.server.controller');

module.exports = function(config) {
  // Use github strategy
  passport.use(new GithubStrategy({
    clientID: config.github.clientID,
    clientSecret: config.github.clientSecret,
    callbackURL: config.github.callbackURL,
    passReqToCallback: true
  },
  function(req, accessToken, refreshToken, profile, done) {
    // Set the provider data and include tokens
    var providerData = profile._json;
    providerData.accessToken = accessToken;
    providerData.refreshToken = refreshToken;

    // Create the user OAuth profile
    var providerUserProfile = {
      displayName: profile.displayName || profile.username,
      email: profile.emails[0].value,
      username: profile.username,
      provider: 'github',
      providerIdentifierField: 'id',
      providerData: providerData
    };

    // Save the user OAuth profile
    users.saveOAuthUserProfile(req, providerUserProfile, done);
  }));
};

'use strict';

/**
 * Module dependencies.
 */
var passport = require('passport'),
    GithubStrategy = require('passport-github').Strategy,
    users = require('../../controllers/users.server.controller');

module.exports = function(config) {
  // Use github strategy
  passport.use(new GithubStrategy({
    clientID: config.github.clientID,
    clientSecret: config.github.clientSecret,
    callbackURL: config.github.callbackURL,
    passReqToCallback: true
  },
  function(req, accessToken, refreshToken, profile, done) {
    // Set the provider data and include tokens
    var providerData = profile._json;
    providerData.accessToken = accessToken;
    providerData.refreshToken = refreshToken;

    // Create the user OAuth profile
    var providerUserProfile = {
      displayName: profile.displayName || profile.username,
      email: (profile.emails && profile.emails.length) ? profile.emails[0].value : undefined,
      username: profile.username,
      provider: 'github',
      providerIdentifierField: 'id',
      providerData: providerData
    };

    // Save the user OAuth profile
    users.saveOAuthUserProfile(req, providerUserProfile, done);
  }));
};

Fix GitHub strategy missing email
Fix GitHub strategy missing email

Fixes an issue with an empty/missing/null Email coming from GitHub's
OAuth call response.
