<commit_before>'use strict';

const Account = require('../../models/account');
const createError = require('http-errors');
const bcrypt = require('bcrypt-as-promised');
const jwt = require('jsonwebtoken-as-promised');

function createToken (req, res, next) {
  Account.findOne({ email: req.body.email })
    .then(ensureAccount)
    .then(compareHashes)
    .then(generateToken)
    .then(sendResponse)
    .catch(onError)
  ;

  function ensureAccount (account) {
    if (!account) {
      throw new createError.Forbidden();
    }

    return account;
  }

  function compareHashes (account) {
    return bcrypt.compare(req.body.password, account.passwordHash);
  }

  function generateToken (account) {
    const payload = {
      id: account.id,
      email: account.email,
      profile: account.profile,
    };

    return jwt.sign(payload, req.app.locals.configuration.jwt.secret, req.app.locals.configuration.jwt.options);
  }

  function sendResponse (token) {
    const jwt = {
      access_token: token,
      token_type: 'bearer'
    };

    res.status(201).json(jwt);
  }

  function onError (err) {
    if (err && err.name === 'MismatchError') {
      return next(new createError.Forbidden(err));
    }

    next(err);
  }
}

module.exports = createToken;<commit_msg>Fix empty payload<commit_after>'use strict';

const Account = require('../../models/account');
const createError = require('http-errors');
const bcrypt = require('bcrypt-as-promised');
const jwt = require('jsonwebtoken-as-promised');

function createToken (req, res, next) {
  Account.findOne({ email: req.body.email })
    .then(ensureAccount)
    .then(compareHashes)
    .then(generateToken)
    .then(sendResponse)
    .catch(next)
  ;

  function ensureAccount (account) {
    if (!account) {
      throw new createError.Forbidden();
    }

    return account;
  }

  function compareHashes (account) {
    return bcrypt.compare(req.body.password, account.passwordHash)
      .then(() => account)
      .catch(err => { throw new createError.Forbidden(err); })
    ;
  }

  function generateToken (account) {
    const payload = {
      id: account.id,
      email: account.email
    };

    return jwt.sign(payload, req.app.locals.configuration.jwt.secret, req.app.locals.configuration.jwt.options);
  }

  function sendResponse (token) {
    const jwt = {
      access_token: token,
      token_type: 'bearer'
    };

    res.status(201).json(jwt);
  }
}

module.exports = createToken;
{'fraction_non_alphanumeric': 0.14042897612302713, 'fraction_numerical': 0.0024281667341157424, 'mean_word_length': 3.182741116751269, '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': '4023094', 'n_tokens_mistral': 803, 'n_tokens_neox': 785, 'n_words': 156}