<commit_before>
'use strict';

var qs    = require('querystring');
var url   = require('url');
var debug = require('debug')('proxive:post');

module.exports = function(config) {
  return function(req, res) {
    var body = '';

    req.on('data', function (data) {
      body += data;

      // Too much POST data, kill the connection!
      // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
      if (body.length > 1e6) {
        req.connection.destroy();
      }
    });

    req.on('end', function () {
      var post = qs.parse(body);

      if(req.url === '/loginWithUsername') {
        debug('/loginWithUsername');
        if(!post.organization) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-organization'
          });
          res.end();
        } else if(!post.username) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-username'
          });
          res.end();
        } else if(!post.password) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-password'
          });
          res.end();
        } else {
          config.users.loginUsername(post.organization, post.username, post.password, function(err, user) {
            if(err) {
              if(err === 'no-user') {
                res.writeHead(302, {
                  location: 'http://'+req.headers.host+'/?err=user-not-found'
                });
                res.end();
              } else {
                console.error(err);
                res.writeHead(302, {
                  location: 'http://'+req.headers.host+'/?err=error-occured'
                });
                res.end();
              }
              return;
            }
            // Set cookie for account security
            req.cookies.set('login.sec', config.users.hmac(user.userId, Math.floor(Date.now()/1000) + 3600, 'secure'), {
              domain: '.' + req.headers.host,
              httpOnly: true,
              secure: (config.development ? false : true)
            });
            // Set cookie for browsing
            req.cookies.set('login', config.users.hmac(user.userId, Math.floor(Date.now()/1000) + 3600), {
              domain: '.' + req.headers.host,
              httpOnly: true
            });
            res.writeHead(302, {
              location: 'http://'+req.headers.host+'/'
            });
            res.end();
          });
        }
      } else if(req.url === '/loginWithEmail') {
        debug('/loginWithEmail');
        if(!post.organization) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-organization'
          });
          res.end();
        } else if(!post.username) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-username'
          });
          res.end();
        } else if(!post.password) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-password'<commit_msg>Login with email + password work<commit_after>
'use strict';

var qs    = require('querystring');
var url   = require('url');
var debug = require('debug')('proxive:post');

module.exports = function(config) {
  return function(req, res) {
    var body = '';

    req.on('data', function (data) {
      body += data;

      // Too much POST data, kill the connection!
      // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
      if (body.length > 1e6) {
        req.connection.destroy();
      }
    });

    req.on('end', function () {
      var post = qs.parse(body);

      if(req.url === '/loginWithUsername') {
        debug('/loginWithUsername');
        if(!post.organization) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-organization'
          });
          res.end();
        } else if(!post.username) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-username'
          });
          res.end();
        } else if(!post.password) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-password'
          });
          res.end();
        } else {
          config.users.loginUsername(post.organization, post.username, post.password, function(err, user) {
            if(err) {
              if(err === 'no-user') {
                res.writeHead(302, {
                  location: 'http://'+req.headers.host+'/?err=user-not-found'
                });
                res.end();
              } else {
                console.error(err);
                res.writeHead(302, {
                  location: 'http://'+req.headers.host+'/?err=error-occured'
                });
                res.end();
              }
              return;
            }
            // Set cookie for account security
            req.cookies.set('login.sec', config.users.hmac(user.userId, Math.floor(Date.now()/1000) + 3600, 'secure'), {
              domain: '.' + req.headers.host,
              httpOnly: true,
              secure: (config.development ? false : true)
            });
            // Set cookie for browsing
            req.cookies.set('login', config.users.hmac(user.userId, Math.floor(Date.now()/1000) + 3600), {
              domain: '.' + req.headers.host,
              httpOnly: true
            });
            res.writeHead(302, {
              location: 'http://'+req.headers.host+'/'
            });
            res.end();
          });
        }
      } else if(req.url === '/loginWithEmail') {
        debug('/loginWithEmail');
        if(!post.email) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-email'
          });
          res.end();
        } else if(!post.password) {
          res.writeHead(302, {
            location: 'http://'+req.headers.host+'/?err=missing-password'
{'fraction_non_alphanumeric': 0.1706049941344059, 'fraction_numerical': 0.0196078431372549, 'mean_word_length': 1.5779697624190065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1139195', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1721, 'n_words': 279}