<commit_before>var Q = require('q');
var jwt = require('jwt-simple');
var mongoose = require('mongoose');

var Users = require('../db/configdb.js').Users;
var Links = require('../links/linksController.js');
var Upvotes = require('../upvotes/upvotesController.js');

var secrets = require('../secrets/secretsController.js');

exports = module.exports = {

  // Promise version of Mongoose's 'Model.findOne()' method.
  findUser: Q.nbind(Users.findOne, Users),
  
  // Promise version of Mongoose's 'Model.create()' method.
  createUser: Q.nbind(Users.create, Users),

  // Return a unique token based on a user document from the database.
  // When decoded later, it will serve as a conditions object for a
  //   database query.
  genToken: function(user) {
    return jwt.encode({
      _id: user._id,
      username: user.username,
      firstname: user.firstname,
      lastname: user.lastname,
    }, secrets.today);
  },

  // Check whether the previously decoded token (stored as 'req.user')
  //   is valid by seeing if it matches a document in the database.
  verifyToken: function (req, res, next) {
    if ( ! req.user ) {
      res.status(403).send();
    } else {
      exports.findUser(req.user)
        .then(function(foundUser) {
          if (foundUser) {
            if(req.makeNewToken === true) {
              res.json({
                username: user.username,
                user_id: user._id,
                token: exports.genToken(user)
              }); 
            }
            next();
          } else {
            res.status(403).send();
          }
        })
        .fail(function(error) {
          next(error);
        });
    }
  },

  // Login a user from a POST request containing a username and a password.
  // If successful, respond with the username and user_id from the database
  //   document as well as an access token.
  login: function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    exports.findUser({ username: username })
      .then(function(user) {
        if ( ! user ) {
          // Bad username
          throw new Error('User does not exist');
        } else if ( ! user.isPassword(password) ) {
          // Bad password
          throw new Error('Incorrect password');
        } else {
          // Success
          res.json({<commit_msg>Changed user to foundUser in usersController<commit_after>var Q = require('q');
var jwt = require('jwt-simple');
var mongoose = require('mongoose');

var Users = require('../db/configdb.js').Users;
var Links = require('../links/linksController.js');
var Upvotes = require('../upvotes/upvotesController.js');

var secrets = require('../secrets/secretsController.js');

exports = module.exports = {

  // Promise version of Mongoose's 'Model.findOne()' method.
  findUser: Q.nbind(Users.findOne, Users),
  
  // Promise version of Mongoose's 'Model.create()' method.
  createUser: Q.nbind(Users.create, Users),

  // Return a unique token based on a user document from the database.
  // When decoded later, it will serve as a conditions object for a
  //   database query.
  genToken: function(user) {
    return jwt.encode({
      _id: user._id,
      username: user.username,
      firstname: user.firstname,
      lastname: user.lastname,
    }, secrets.today);
  },

  // Check whether the previously decoded token (stored as 'req.user')
  //   is valid by seeing if it matches a document in the database.
  verifyToken: function (req, res, next) {
    if ( ! req.user ) {
      res.status(403).send();
    } else {
      exports.findUser(req.user)
        .then(function(foundUser) {
          if (foundUser) {
            if(req.makeNewToken === true) {
              res.json({
                username: foundUser.username,
                user_id: foundUser._id,
                token: exports.genToken(foundUser)
              }); 
            }
            next();
          } else {
            res.status(403).send();
          }
        })
        .fail(function(error) {
          next(error);
        });
    }
  },

  // Login a user from a POST request containing a username and a password.
  // If successful, respond with the username and user_id from the database
  //   document as well as an access token.
  login: function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    exports.findUser({ username: username })
      .then(function(user) {
        if ( ! user ) {
          // Bad username
          throw new Error('User does not exist');
        } else if ( ! user.isPassword(password) ) {
          // Bad password
          throw new Error('Incorrect password');
        } else {
          // Success
          res.json({
{'fraction_non_alphanumeric': 0.12620950778291964, 'fraction_numerical': 0.002524190155658393, 'mean_word_length': 2.136543535620053, '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': '3690260', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1350, 'n_words': 396}