<commit_before>var bcrypt = require('bcrypt');

module.exports = function (yeti) {

  return {

    postRegister: function (req, res) {
    },

    putLogin: function (req, res) {
      if (!req.params.username || !req.params.password) {
        res.writeHead(403);
        return res.end('errrrrorrrr');
      }
      db.view('users', 'password_by_username',
        { keys: [req.params.username] },
        function (err, body) {
          if (err) {
            res.writeHead(500);
            return res.end(err);
          }

          if (!body.rows.length) {
            res.writeHead(404);
            return res.end('nuh uh');
          }
          
          bcrypt.compare(req.params.password,
            body.rows[0].value,
            function (err, matches) {
              if (!matches) {
                res.writeHead(403);
                return res.end('nope');
              }
              rs.create({ app: 'yeti', id: req.params.username },
                function (err, resp) {
                  if (err) {
                    res.writeHead(500);
                    res.end('nope');
                  }
                  res.end(JSON.stringify({ success: true, token: resp.token }));
                });
            });
        });
    },

    putLogout: function (req, res) {
      rs.kill({ app: 'yeti', token: req.session.token },
        function (err, resp) {
          if (err || !resp || !resp.kill) {
            res.writeHead(500);
            return res.end('Bad');
          };
          return res.end(JSON.stringify({ success: true }));
        });<commit_msg>use yeti response objects<commit_after>var bcrypt = require('bcrypt');

module.exports = function (yeti) {

  return {

    postRegister: function (req, res) {
    },

    putLogin: function (req, res) {
      if (!req.params.username || !req.params.password) return yeti.sendJson(403, new yeti.Error('Send credetials')); 
      db.view('users', 'password_by_username',
        { keys: [req.params.username] },
        function (err, body) {
          if (err) return yeti.sendJson(500, new yeti.Error('Database error'));

          if (!body.rows.length) return yeti.sendJson(404, new yeti.Error('No such user')); 
          
          bcrypt.compare(req.params.password,
            body.rows[0].value,
            function (err, matches) {
              if (!matches) return yeti.sendJson(403, new yeti.Error('Authentication failed')); 
              rs.create({ app: 'yeti', id: req.params.username },
                function (err, resp) {
                  if (err) return yeti.sendJson(500, new yeti.Error('Session error')); 
                  yeti.sendJson(new yeti.Response({ token: resp.token }));
                });
            });
        });
    },

    putLogout: function (req, res) {
      rs.kill({ app: 'yeti', token: req.session.token },
        function (err, resp) {
          if (err || !resp || !resp.kill) {
            res.writeHead(500);
            return res.end('Bad');
          };
          return res.end(JSON.stringify({ success: true }));
        });
{'fraction_non_alphanumeric': 0.15050275705481675, 'fraction_numerical': 0.012325656827765165, 'mean_word_length': 1.6632124352331605, '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': '3253338', 'n_tokens_mistral': 933, 'n_tokens_neox': 884, 'n_words': 191}