<commit_before>
/**
 * Module dependencies.
 */

var bcrypt = require('bcrypt-nodejs')
  , MongoClient = require('mongodb').MongoClient
  , passport = require('passport');

exports.register = function(req, res) {
  var warn = [ 'Password is transmitted in plain-text!' ];
  res.render('team/register', { warn: warn });
};

exports.doRegister = function(req, res, next) {
  var username = req.body.username
    , password = <PASSWORD>;

  MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    if (err) {
      return next(err);
    }

    var teams = db.collection('teams');

    bcrypt.genSalt(10, function(err, salt) {
      if (err) {
        return next(err);
      }

      bcrypt.hash(password, salt, null, function(err, crypted) {
        if (err) {
          return next(err);
        }

        delete req.body.password;
        var team = { name: username, hash: crypted };

        teams.insert(team, { w: 1 }, function(err, docs) {
          // Check for duplicate username
          if (err && err.message.indexOf('E11000 ') !== -1) {
            req.flash('error', '%s is already taken.', username);
            return res.redirect('/register');
          }

          // Some other kind of error
          else if (err) {
            return next(err);
          }

          req.login(docs[0], function(err) {
            if (err) {
              return next(err);
            }

            req.flash('log', 'Successfully registered as team %s.', docs[0].name);
            res.redirect('/');
          });
        });
      });
    });
  });
};

exports.login = function(req, res) {
  var warn = [ 'Password is transmitted in plain-text!' ];
  res.render('team/login', { warn: warn });
};

exports.doLogin = function(req, res) {
  passport.authenticate('local', function(err, user, info) {
    if (err) {
      return next(err);
    }

    // Failed to authenticate
    if (!user) {
      req.flash('error', 'Invalid team name or password.');<commit_msg>Created uploads folder for team when registered.<commit_after>
/**
 * Module dependencies.
 */

var fs = require('fs')
  , path = require('path');

var bcrypt = require('bcrypt-nodejs')
  , MongoClient = require('mongodb').MongoClient
  , passport = require('passport');

/*
 * Constructs a team document of the following form.
 *   { name: ..., hash: ... }
 *
 * callback = function(error, result)
 */
function makeTeam(username, password, callback) {
  bcrypt.genSalt(10, function(err, salt) {
    if (err) {
      return callback(err, undefined);
    }

    bcrypt.hash(password, salt, null, function(err, crypted) {
      if (err) {
        return callback(err, undefined);
      }

      var team = { name: username, hash: crypted };

      return callback(null, team);
    });
  });
};

exports.register = function(req, res) {
  var warn = [ 'Password is transmitted in plain-text!' ];
  res.render('team/register', { warn: warn });
};

exports.doRegister = function(req, res, next) {
  var username = req.body.username
    , password = <PASSWORD>;

  delete req.body.password;

  if (!/^\w+$/.test(username)) {
    req.flash('error', 'Team name must contain only alphanumeric characters')
    return res.redirect('/register');
  }

  makeTeam(username, password, function(err, team) {
    if (err) {
      return next(err);
    }

    function callback(err) {
      db.close();
      return next(err);
    };

    MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
      if (err) {
        return callback(err);
      }

      var teams = db.collection('teams');

      teams.insert(team, { w: 1 }, function(err, docs) {
        // Check for duplicate username
        if (err && err.message.indexOf('E11000 ') !== -1) {
          req.flash('error', 'Team %s is already taken.', username);
          return res.redirect('/register');
        }

        // Some other kind of error
        else if (err) {
          return callback(err);
        }

        // Make directory for submissions
        var dir = path.join('private', 'uploads', team.name);

        // TODO: handle case where inserting team into database
        //       succeeds, but creating folder fails
        fs.mkdirSync(dir, 0755);

        req.login(docs[0], function(err) {
          if (err) {
            return callback(err);
          }

          req.flash('log', 'Successfully registered as team %s.', docs[0].name);
          res.redirect('/');
        });
      });
    });
  });
};

exports.login = function(req, res) {
  var warn = [ 'Password is transmitted in plain-text!' ];
  res.render('team/login', { warn: warn });
};

exports.doLogin = function(req, res) {
  passport.authenticate('local', function(err, user, info) {
    if (err) {
      return next(err);
    }

    // Failed to authenticate
    if (!user) {
      req.flash('error', 'Invalid team name or password.');
{'fraction_non_alphanumeric': 0.15701125895598772, 'fraction_numerical': 0.00736949846468782, 'mean_word_length': 2.3465753424657536, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3045895', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1449, 'n_words': 374}