<commit_before>const bcrypt = require('bcryptjs');
const LocalStrategy = require('passport-local').Strategy;
const mongo = require('./db');
const ObjectID = require('mongodb').ObjectID;
const passport = require('passport');

passport.use(new LocalStrategy(authenticate));
passport.use("local-register", new LocalStrategy({
  passReqToCallback: true
}, register))

function authenticate(email, password, done) {
  mongo.db.collection('users')
  .findOne({ email: email }, (err, user) => {
    if (err) return done(err);
    if (!user) return done(null, false, { message: "Incorrect email or password." });

    bcrypt.compare(password, user.password, (err, passwordMatches) => {
      if (passwordMatches === false) {
        return done(null, false, { message: "Incorrect email or password." });
      }else {
        return done(null, user);
      }
    });
  });
}

function register(req, email, password, done) {
  if (req.body.password2 !== password) {
    return done(null, false, { message: "Passwords do not match." });
  }

  mongo.db.collection('users')
  .findOne({ email: email }, (err, user) => {
    if (err) return done(err);
    if (user) {
      return done(null, false, { message: "User with that email already exists." });
    }

    bcrypt.hash(password, 8, (err, hashedPassword) => {
      if (err) return done(err);
      const newUser = {
        email: email,
        password: <PASSWORD>,
        joinDate: new Date()
      }

      mongo.db.collection('users')
      .insert(newUser, (err, result) => {
        if (err) return done(err);
        return done(null, result.ops[0]);
      });
    });
  });
}

passport.serializeUser(function(user, done) {
  done(null, user._id);
});

passport.deserializeUser(function(id, done) {<commit_msg>small change<commit_after>const bcrypt = require('bcryptjs');
const LocalStrategy = require('passport-local').Strategy;
const mongo = require('./db');
const ObjectID = require('mongodb').ObjectID;
const passport = require('passport');

passport.use(new LocalStrategy(authenticate));
passport.use("local-register", new LocalStrategy({
  passReqToCallback: true
}, register))

function authenticate(email, password, done) {
  mongo.db.collection('users')
  .findOne({ email: email }, (err, user) => {
    if (err) return done(err);
    if (!user) return done(null, false, { message: "Incorrect email or password." });

    bcrypt.compare(password, user.password, (err, passwordMatches) => {
      if (passwordMatches === false) {
        return done(null, false, { message: "Incorrect email or password." });
      }else {
        return done(null, user);
      }
    });
  });
}

function register(req, email, password, done) {
  if (req.body.password2 !== password) {
    return done(null, false, { message: "Passwords do not match." });
  }

  mongo.db.collection('users')
  .findOne({ email: email }, (err, user) => {
    if (err) return done(err);
    if (user) {
      return done(null, false, { message: "User with that email already exists." });
    }

    bcrypt.hash(password, 8, (err, hashedPassword) => {
      if (err) return done(err);
      const newUser = {
        email: email,
        password: <PASSWORD>,
        joinDate: new Date()
      }

      mongo.db.collection('users')
      .insert(newUser, (err, result) => {
        if (err) return done(err);
        return done(null, newUser);
      });
    });
  });
}

passport.serializeUser(function(user, done) {
  done(null, user._id);
});

passport.deserializeUser(function(id, done) {
{'fraction_non_alphanumeric': 0.16330587901164442, 'fraction_numerical': 0.0014200511218403862, 'mean_word_length': 3.3751552795031055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '285687', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1088, 'n_words': 277}