<commit_before>import router from 'koa-router';

import User from '../models/user';
import mongo from '../../config/mongo';
import ws from '../../config/socket';

const local = 'local-signup';

function* createUser() {
  const user = this.request.body;
  user._id = yield mongo.getNextSequence('userId');
  const results = yield mongo.users.insertOne(user);
  this.status = 201;
  this.body = { id: results.ops[0]._id };
}
function* signIn(next) {
  yield next
  res.json(this.user);
}

function* signOut(next) {
  yield next
  this.logout();
  res.end();
}

function* getCredentials(next) {
  yield next
  res.json(this.user);
}

function* getAllUsernames(next) {
  yield next
  User.find(
    { 'local.username': { $exists: true } },
    { 'local.username': 1, _id: 0 },
    (err, data) => {
      if (err) {
        console.log(err);
        return res.status(500).json({ msg: 'internal server error' });
      }
      res.json(data);
    },
  );
}

const user = new router();

user.get('/auth/signup', createUser);
user.get('/auth/signin', signIn);<commit_msg>refactor(user controller): refactor controller to koa style<commit_after>import router from 'koa-router';

import User from '../models/user';
import ws from '../../config/socket';

function* createUser() {
  const ctx = this
  const newUser = new User({
    username: ctx.request.body.username,
    password: ctx.request.body.password,
    email: ctx.request.body.email
  })
  console.log('line:  ', newUser);
  try {
    yield* newUser.save(newUser)
    ctx.response.status = 201;
  } catch (e) {
    ctx.throw(500, e)
  }
}

function* signIn(next) {
  yield next
  res.json(this.user);
}

function* signOut(next) {
  yield next
  this.logout();
  res.end();
}

function* getCredentials(next) {
  yield next
  res.json(this.user);
}

function* getAllUsernames() {
  const ctx = this
  yield User
    .find({})
    .select('username')
    .exec()
}

const user = new router();

user.post('/auth/signup', createUser);
user.get('/auth/signin', signIn);
{'fraction_non_alphanumeric': 0.17030784508440913, 'fraction_numerical': 0.007447864945382324, 'mean_word_length': 3.29637526652452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5755369', 'n_tokens_mistral': 708, 'n_tokens_neox': 701, 'n_words': 161}