<commit_before>const debug = require('debug');
const PassportLocalStrategy = require('passport-local').Strategy;

const models = require('../models');

const User = models.User;

module.exports = new PassportLocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    session: false,
    passReqToCallback: true,
  },
  (req, email, password, done) => {
    const userData = {
      email: email.trim(),
      password: <PASSWORD>(),
      name: req.body.name.trim(),
    };

    return User.create(userData)
      .then((newUser) => {
        if (newUser) {
          debug('In configurePassport.createUser. newUser was created. newUser is ', newUser);
          return done(null, newUser);
        }
        debug('In configurePassport.createUser. newUser was NOT created.');
        return done(null, false);
      })
      .catch(error => done(error, false, { message: 'error in local-signup' }));
  },
);
<commit_msg>change: (preemptive bug fix) proactively find user before creating it to handle duplicate signups at the code level instead of at the DB-ORM (Sequelize) level which throws error: duplicate due to DB unique column constraint<commit_after>const debug = require('debug');
const PassportLocalStrategy = require('passport-local').Strategy;

const models = require('../models');

const User = models.User;

module.exports = new PassportLocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    session: false,
    passReqToCallback: true,
  },
  (req, email, password, done) => {
    const userData = {
      email: email.trim(),
      password: <PASSWORD>(),
      name: req.body.name.trim(),
    };

    User.findOne({ where: { username: email } })
      .then((existingUser) => {
        if (existingUser) {
          return done(null, false, { message: `user with email ${email} already exists` });
        }
        return existingUser;
      })
      .then(User.create(userData))
      .catch(error => done(error, false, { message: 'error in local-signup' }))
      .then((newUser) => {
        if (newUser) {
          debug('In configurePassport.createUser. newUser was created. newUser is ', newUser);
          return done(null, newUser);
        }
        debug('In configurePassport.createUser. newUser was NOT created.');
        return done(null, false);
      })
      .catch(error => done(error, false, { message: 'unknown error in local-signup' }));
  },
);

{'fraction_non_alphanumeric': 0.13828477636438244, 'fraction_numerical': 0.0, 'mean_word_length': 2.957792207792208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4425772', 'n_tokens_mistral': 703, 'n_tokens_neox': 703, 'n_words': 194}