<commit_before>const bcrypt = require('bcryptjs');
const { hasSession, clearSession, createSession } = require('../auth/sessionHelpers');
const { users, instruments } = require('../db/models');

// GET `/api/accounts/logout`
module.exports.logout = (req, res) => {
  if (hasSession(req)) {
    clearSession(req);
  }
  req.logout();
  res.sendStatus(200);
};

// POST `/api/accounts/login`
module.exports.login = (req, res) => {
  const { user, pass } = req.body;

  users.findOne({
    where: {
      userName: user
    }
  }).then(person => {
    if (!person) {
      res.redirect('/api/accounts/login');
    } else {
      bcrypt.compare(pass, user.hash, (err, matches) => {
        if (!matches) {
          res.redirect('/api/accounts/login');
        } else {
          instruments.findAll({ where: { userName: person.userName }})
            .then(collection => {
              const userInstruments = collection.map(inst => inst.dataValues);
              createSession(req, user);
              res.status(200).json(userInstruments);
            });
        }
      });
    }
  });
};

// POST `/api/accounts`
module.exports.signup = (req, res) => {
  const userName = req.body.user; // TODO: refactor client to send better-named parameters
  const password = req.body.pass;

  users.findOne({ where: { userName } })
    .then(user => {
      if (user) {
        res.status(200).json('User already exists by the name', userName);
      } else {
        // TODO: user asynchronous methods instead
        const salt = bcrypt.genSaltSync(10);
        const hash = bcrypt.hashSync(password, salt);

        users.create({
          userName,
          password: hash,
          salt,
        }).then(entry => {
          createSession(req, userName);
          // TODO: redirect user somewhere cool
          res.status(201).json('Successfully signed up');
        });
      }
    });
};<commit_msg>Refactor handlers for /api/accounts<commit_after>const bcrypt = require('bcryptjs');
const { hasSession, clearSession, createSession } = require('../auth/sessionHelpers');
const { User, Instrument } = require('../db/models');

// GET `/api/accounts/logout`
module.exports.logout = (req, res) => {
  if (hasSession(req)) {
    clearSession(req);
  }
  req.logout();
  res.sendStatus(200);
};

// POST `/api/accounts/login`
module.exports.login = (req, res) => {
  const { username, password } = req.body;

  // TODO: eager load the user instruments
  users.findOne({
    where: { name: username }
  }).then(person => {
    if (!person) {
      res.status(400).json('Wrong username/password combination');
    } else {
      bcrypt.compare(password, person.hashed_password, (err, matches) => {
        if (!matches) {
          res.status(400).json('Wrong username/password combination');
        } else {
          Instrument.findAll({ where: { user_id: person.id }})
            .then(collection => {
              const userInstruments = collection.map(inst => inst.dataValues);
              createSession(req, username);
              res.status(200).json(userInstruments);
            });
        }
      });
    }
  });
};

// POST `/api/accounts`
module.exports.signup = (req, res) => {
  const { username, password } = req.body;

  User.findOne({ where: { name: username } })
    .then(user => {
      if (user) {
        const msg = `User already exists by the name, ${username}`;
        res.status(400).json(msg);
      } else {
        // TODO: user asynchronous methods instead
        const salt = bcrypt.genSaltSync(10);
        const hash = bcrypt.hashSync(password, salt);

        User.create({
          name: username,
          hashed_password: hash,
          salt,
        }).then(newUser => {
          createSession(req, newUser.name);
          // TODO: redirect user somewhere cool
          res.status(201).json('Successfully signed up');
        });
      }
    });
};
{'fraction_non_alphanumeric': 0.15765188413227377, 'fraction_numerical': 0.008715713919507819, 'mean_word_length': 2.298393913778529, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '623870', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1168, 'n_words': 249}