<commit_before>'use strict';

const _ = require('underscore');
const app = require('express').Router();
const jwt = require('jsonwebtoken');

const config = require('../../config/config');
const User = require('../../models/userModel');
const checkLoginParams = require('../../utils/auth').checkLoginParams;

app.post('/login', checkLoginParams, (req, res, next) => {
  const opts = { [req.loginType]: req.body[req.loginType] };

  User.findOne(opts, (err, user) => {
    if (err) {
      return next(err);
    }

    if (!user) {
      return next(new Error('Invalid username or email'));
    }

    if (!user.checkPassword(req.body.password)) {
      return next(new Error('Invalid password'));
    } else {
      const token = jwt.sign(
        user._doc,
        process.env.JWT || config.secrets.jwt,
        { expiresIn: '90 days' }
      );

      if (!token) {
        return next(new Error('Error generating authentication token'));
      }

      res.json({
        user: _.omit(user._doc, 'password'),
        token: 'JWT ' + token
      });
    }
  });
});

module.exports = app;<commit_msg>Move auth API routes to use Promises<commit_after>'use strict';

const _ = require('underscore');
const app = require('express').Router();
const jwt = require('jsonwebtoken');

const config = require('../../config/config');
const User = require('../../models/userModel');
const checkLoginParams = require('../../utils/auth').checkLoginParams;

app.post('/login', checkLoginParams, (req, res, next) => {
  User
    .findOne({ [req.loginType]: req.body[req.loginType] })
    .exec()
    .then(checkValidCredentials.bind(null, req.body.password))
    .then((user) => {
      const token = jwt.sign(
        user._doc,
        process.env.JWT || config.secrets.jwt,
        { expiresIn: '90 days' }
      );

      if (!token) {
        return Promise.reject(
          new Error('Error generating authentication token')
        );
      }

      res.json({
        user: _.omit(user._doc, 'password'),
        token: 'JWT ' + token
      });
    })
    .catch(next);
});

function checkValidCredentials(password, user) {
  if (!user) {
    return Promise.reject(new Error('Invalid username or email'));
  }

  if (!user.checkPassword(password)) {
    return Promise.reject(new Error('Invalid password'));
  }

  return user;
}

module.exports = app;
{'fraction_non_alphanumeric': 0.17454235845040442, 'fraction_numerical': 0.0017028522775649213, 'mean_word_length': 2.7964458804523424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6110977', 'n_tokens_mistral': 726, 'n_tokens_neox': 707, 'n_words': 160}