<commit_before>var express           = require('express');
var config            = require(__dirname + '/../config.js');
var router            = express.Router();
var passport          = require('passport');
var GitHubStrategy    = require('passport-github').Strategy;
var BitbucketStrategy = require('passport-bitbucket').Strategy;
var GoogleStrategy    = require('passport-google-oauth').OAuth2Strategy;
var user              = require(__dirname + '/../lib/user');

// Clean up the user object
function cleanUpUserObject(userdoc) {
  userdoc.id = userdoc._id;
  delete userdoc._id;
  delete userdoc._rev;
  return userdoc;
}

// Passport session setup.
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(obj, done) {
  user.get(obj, function(err, body) {
    if (err) {
      var userdoc = {}
    } else {
      var userdoc = cleanUpUserObject(body);
    }

    return done(null, userdoc);
  });
});

// Use the GitHubStrategy within Passport.
passport.use(new GitHubStrategy({
    clientID: config.github.client_id,
    clientSecret: config.github.client_secret,
    callbackURL: config.github.client_callback
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function() {

      var options = {
        key: profile._json.email,
        include_docs: true
      };
      user.view('user', 'byemail', options, function(err, data) {
        if (err) return false;

        // Create user if doesn't exist
        if (!data.rows.length) {
          var doc = {
            name: profile._json.name,
            email: profile._json.email,
            avatar: profile._json.avatar_url,
            url: profile._json.blog,
            github: profile._json.login,
            company: profile._json.company,
            auth: {
              github : profile._json
            }
          }
          user.insert(doc, function(err, userdata) {
            user.get(userdata.id, function(err, body) {
              return done(null, cleanUpUserObject(body));
            });
          })
        } else {
          var userdoc = data.rows[0].doc;
          userdoc.id = userdoc._id;
          userdoc.github = userdoc.github || profile._json.login;
          userdoc.company = userdoc.company || profile._json.company;

          // Update the auth data
          if (!('auth' in userdoc)) userdoc.auth = {}
          if (!('github' in userdoc.auth)) userdoc.auth.github = {}
          userdoc.auth.github = profile._json
          user.insert(userdoc, userdoc._id)

          return done(null, cleanUpUserObject(userdoc));
        }

      });
    });
  }

));

// Use the GoogleStrategy within Passport.
passport.use(new GoogleStrategy({
    clientID: config.google.consumer_key,
    clientSecret: config.google.consumer_secret,
    callbackURL: config.google.consumer_callback
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function() {
      var email = profile.emails[0].value;
      var options = {
        key: email,
        include_docs: true
      };
      user.view('user', 'byemail', options, function(err, data) {
        if (err) return false;

        // Create user if doesn't exist
        if (!data.rows.length) {
          var doc = {
            name: profile.displayName,
            email: email,
            avatar: profile.photos[0].value,<commit_msg>Add support for when GitHub doesn't provide an e-mail<commit_after>var express           = require('express');
var config            = require(__dirname + '/../config.js');
var router            = express.Router();
var passport          = require('passport');
var GitHubStrategy    = require('passport-github').Strategy;
var BitbucketStrategy = require('passport-bitbucket').Strategy;
var GoogleStrategy    = require('passport-google-oauth').OAuth2Strategy;
var user              = require(__dirname + '/../lib/user');

// Clean up the user object
function cleanUpUserObject(userdoc) {
  userdoc.id = userdoc._id;
  delete userdoc._id;
  delete userdoc._rev;
  return userdoc;
}

// Passport session setup.
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(obj, done) {
  user.get(obj, function(err, body) {
    if (err) {
      var userdoc = {}
    } else {
      var userdoc = cleanUpUserObject(body);
    }

    return done(null, userdoc);
  });
});

// Use the GitHubStrategy within Passport.
passport.use(new GitHubStrategy({
    clientID: config.github.client_id,
    clientSecret: config.github.client_secret,
    callbackURL: config.github.client_callback
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function() {

      if (profile._json.email) {
        var options = {
          key: profile._json.email,
          include_docs: true
        };
        user.view('user', 'byemail', options, function(err, data) {
          if (err) return false;

          // Create user if doesn't exist
          if (!data.rows.length) {
            var doc = {
              name: profile._json.name,
              email: profile._json.email,
              avatar: profile._json.avatar_url,
              url: profile._json.blog,
              github: profile._json.login,
              company: profile._json.company,
              auth: {
                github : profile._json
              }
            }
            user.insert(doc, function(err, userdata) {
              user.get(userdata.id, function(err, body) {
                return done(null, cleanUpUserObject(body));
              });
            })
          } else {
            var userdoc = data.rows[0].doc;
            userdoc.id = userdoc._id;
            userdoc.github = userdoc.github || profile._json.login;
            userdoc.company = userdoc.company || profile._json.company;

            // Update the auth data
            if (!('auth' in userdoc)) userdoc.auth = {}
            if (!('github' in userdoc.auth)) userdoc.auth.github = {}
            userdoc.auth.github = profile._json
            user.insert(userdoc, userdoc._id)

            return done(null, cleanUpUserObject(userdoc));
          }

        });
      } else {
        var options = {
          key: ['github',profile._json.id],
          include_docs: true
        };
        user.view('user', 'byauth', options, function(err, data) {
          if (err) return false;

          // Create user if doesn't exist
          if (!data.rows.length) {
            var doc = {
              name: profile._json.name,
              email: profile._json.email,
              avatar: profile._json.avatar_url,
              url: profile._json.blog,
              github: profile._json.login,
              company: profile._json.company,
              auth: {
                github : profile._json
              }
            }
            user.insert(doc, function(err, userdata) {
              user.get(userdata.id, function(err, body) {
                return done(null, cleanUpUserObject(body));
              });
            })
          } else {
            var userdoc = data.rows[0].doc;
            userdoc.id = userdoc._id;
            userdoc.github = userdoc.github || profile._json.login;
            userdoc.company = userdoc.company || profile._json.company;

            // Update the auth data
            if (!('auth' in userdoc)) userdoc.auth = {}
            if (!('github' in userdoc.auth)) userdoc.auth.github = {}
            userdoc.auth.github = profile._json
            user.insert(userdoc, userdoc._id)

            return done(null, cleanUpUserObject(userdoc));
          }

        });
      }
    });
  }

));

// Use the GoogleStrategy within Passport.
passport.use(new GoogleStrategy({
    clientID: config.google.consumer_key,
    clientSecret: config.google.consumer_secret,
    callbackURL: config.google.consumer_callback
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function() {
      var email = profile.emails[0].value;
      var options = {
        key: email,
        include_docs: true
      };
      user.view('user', 'byemail', options, function(err, data) {
        if (err) return false;

        // Create user if doesn't exist
        if (!data.rows.length) {
          var doc = {
            name: profile.displayName,
            email: email,
            avatar: profile.photos[0].value,
{'fraction_non_alphanumeric': 0.1218321320623111, 'fraction_numerical': 0.0010462683096954196, 'mean_word_length': 2.014365802382621, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4351856', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2380, 'n_words': 535}