<commit_before>import { setAuthTokenCookieForUserId } from '../auth-token';
import connect from '../connection';
import getUserByEmail from '../loaders/get-user-by-email';
import getUserByName from '../loaders/get-user-by-name';
import onError from '../util/on-error';
import { validate, NAME_REX, EMAIL_REX, PASSWORD_REX } from '../validator';
import { createPassHash } from '../passhash';

export default onError(async function createAccount(req, res) {
  let name = req.body.name || '';
  name = name.trim();
  let email = req.body.email || '';
  email = email.trim();
  const password = req.body.password || '';

  if (!name || !validate(NAME_REX, name)) {
    return res.json({ success: false, nameError: 'Invalid name' });
  }
  if (!email || !validate(EMAIL_REX, email)) {
    return res.json({ success: false, emailError: 'Invalid email' });
  }
  if (!password || !validate(PASSWORD_REX, password)) {
    return res.json({ success: false, passwordError: 'Invalid password' });
  }

  const passHash = createPassHash(name, password);
  const color = passHash.substr(0, 6);
  const db = connect();

  const emailUser = await getUserByEmail(email, db);
  if (emailUser) {
    return res.json({ success: false, emailError: 'Email is already taken.' });
  }
  const nameUser = await getUserByName(name, db);
  if (nameUser) {
    return res.json({ success: false, nameError: 'Name is already taken.' });
  }

  const insertResult = await db.one(
    `insert into t_user (name, email, passhash, color)
      values ($(name), $(email), $(passHash), $(color))
      returning id, status`,
    { name, email, passHash, color }
  );<commit_msg>Allowing new signups to post<commit_after>import { setAuthTokenCookieForUserId } from '../auth-token';
import connect from '../connection';
import getUserByEmail from '../loaders/get-user-by-email';
import getUserByName from '../loaders/get-user-by-name';
import onError from '../util/on-error';
import { validate, NAME_REX, EMAIL_REX, PASSWORD_REX } from '../validator';
import { createPassHash } from '../passhash';

export default onError(async function createAccount(req, res) {
  let name = req.body.name || '';
  name = name.trim();
  let email = req.body.email || '';
  email = email.trim();
  const password = req.body.password || '';

  if (!name || !validate(NAME_REX, name)) {
    return res.json({ success: false, nameError: 'Invalid name' });
  }
  if (!email || !validate(EMAIL_REX, email)) {
    return res.json({ success: false, emailError: 'Invalid email' });
  }
  if (!password || !validate(PASSWORD_REX, password)) {
    return res.json({ success: false, passwordError: 'Invalid password' });
  }

  const passHash = createPassHash(name, password);
  const color = passHash.substr(0, 6);
  const db = connect();

  const emailUser = await getUserByEmail(email, db);
  if (emailUser) {
    return res.json({ success: false, emailError: 'Email is already taken.' });
  }
  const nameUser = await getUserByName(name, db);
  if (nameUser) {
    return res.json({ success: false, nameError: 'Name is already taken.' });
  }

  const insertResult = await db.one(
    `insert into t_user (name, email, passhash, color, status)
      values ($(name), $(email), $(passHash), $(color), 1)
      returning id, status`,
    { name, email, passHash, color }
  );
{'fraction_non_alphanumeric': 0.15942028985507245, 'fraction_numerical': 0.0015096618357487923, 'mean_word_length': 3.9521674140508223, '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': '1749083', 'n_tokens_mistral': 1032, 'n_tokens_neox': 1028, 'n_words': 304}