<commit_before>var User = require("./userModel");
var bluebird = require("bluebird");
var jwt = require("jsonwebtoken");
var helpers = require("../config/helpers");

bluebird.promisifyAll(User);
var TWO_WEEKS_IN_MINUTES = 60 * 24 * 14;

module.exports = {
  signin: function(request, response, next) {
    var username = request.body.username;
    var password = request.body.password;
    User.model.findOneAsync({username: username})
      .then(function(user) {
        if (user) {
          if (User.validPasswordAsync(password, user.password)) {
            response.status(200).send({
              message: "signed in!",
              token: jwt.sign(user.username, process.env.JWT_SECRET,
                {expiresInMinutes: TWO_WEEKS_IN_MINUTES})
            });
            next();
          } else {
            helpers.errorHandler("invalid password", request, response, next);
          }
        } else {
          helpers.errorHandler("invalid username", request, response, next);
        }
      })
      .catch(function(error) {
        helpers.errorLogger(error, request, response, next);
        helpers.errorHandler(error, request, response, next);
      });
  },

  signup: function(request, response, next) {
    var newUser = request.body.username;
    var password = request.body.password;
    if (!newUser || !password) {
      helpers.errorHandler("Invalid username/password input", request, response, next);
    }
    User.model.findOneAsync({username: newUser})
      .then(function(user) {
        //enable the following log statement for troubleshooting purposes
        //console.debug("findOneAsync args:", arguments);
        if (user) {
          helpers.errorHandler("User already exists", request, response, next);
        } else {
          User.createHash(password, function(error, hash) {
            User.model.createAsync({
              username: newUser,
              password: <PASSWORD>
            })
            .then(function(user) {
              response.status(201).send({
                token: jwt.sign(user.username, process.env.JWT_SECRET,
                  {expiresInMinutes: TWO_WEEKS_IN_MINUTES})
              });
            })
            .catch(function(error) {
              helpers.errorHandler(error, request, response, next);
            });
          });
        }
      })
      .catch(function(error) {
        helpers.errorHandler(error, request, response, next);
      });
  },

  checkAuth: function(request, response, next) {
    //TODO: still need communication from client side to test sent headers (#91)
    //this will be whatever header we put the jwt under.
    var token = request.headers["x-jwt"];
    var user = jwt.verify(token, process.env.JWT_SECRET);
  }
};<commit_msg>fixes problems with errorHandling, including not breaking out of functions on errors and not sending errors correctly<commit_after>var User = require("./userModel");
var bluebird = require("bluebird");
var jwt = require("jsonwebtoken");
var helpers = require("../config/helpers");

bluebird.promisifyAll(User);
var TWO_WEEKS_IN_MINUTES = 60 * 24 * 14;

module.exports = {
  signin: function(request, response, next) {
    var error;
    var username = request.body.username;
    var password = request.body.password;
    User.model.findOneAsync({username: username})
      .then(function(user) {
        if (user) {
          if (User.validPasswordAsync(password, user.password)) {
            response.status(200).send({
              message: "signed in!",
              token: jwt.sign(user.username, process.env.JWT_SECRET,
                {expiresInMinutes: TWO_WEEKS_IN_MINUTES})
            });
            next();
          } else {
            error = {message: "invalid password"};
            helpers.errorHandler(error, request, response, next);
            return;
          }
        } else {
          error = {message: "invalid username"};
          helpers.errorHandler(error, request, response, next);
          return;
        }
      })
      .catch(function(error) {
        helpers.errorLogger(error, request, response, next);
        helpers.errorHandler(error, request, response, next);
        return;
      });
  },

  signup: function(request, response, next) {
    var error;
    var newUser = request.body.username;
    var password = request.body.password;
    if (!newUser || !password) {
      error = {message: "Invalid username/password input"};
      helpers.errorHandler(error, request, response, next);
      return;
    }
    User.model.findOneAsync({username: newUser})
      .then(function(user) {
        //enable the following log statement for troubleshooting purposes
        //console.debug("findOneAsync args:", arguments);
        if (user) {
          error = {message: "User already exists"};
          helpers.errorHandler(error, request, response, next);
          return;
        } else {
          User.createHash(password, function(error, hash) {
            User.model.createAsync({
              username: newUser,
              password: <PASSWORD>
            })
            .then(function(user) {
              response.status(201).send({
                token: jwt.sign(user.username, process.env.JWT_SECRET,
                  {expiresInMinutes: TWO_WEEKS_IN_MINUTES})
              });
            })
            .catch(function(error) {
              helpers.errorHandler(error, request, response, next);
              return;
            });
          });
        }
      })
      .catch(function(error) {
        helpers.errorHandler(error, request, response, next);
        return;
      });
  },

  checkAuth: function(request, response, next) {
    //TODO: still need communication from client side to test sent headers (#91)
    //this will be whatever header we put the jwt under.
    var token = request.headers["x-jwt"];
    var user = jwt.verify(token, process.env.JWT_SECRET);
  }
};
{'fraction_non_alphanumeric': 0.12280108254397835, 'fraction_numerical': 0.004736129905277402, 'mean_word_length': 2.2453347969264543, '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': '6250336', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1646, 'n_words': 375}