<commit_before>const User = require('../../../lib/domain/models/data/user');

const jsonwebtoken = require('jsonwebtoken');

const encrypt = require('../../domain/services/encryption-service');
const validationErrorSerializer = require('../../infrastructure/serializers/jsonapi/validation-error-serializer');
const settings = require('../../settings');

const Authentication = require('../../domain/models/data/authentication');
const authenticationSerializer = require('../../infrastructure/serializers/jsonapi/authentication-serializer');

module.exports = {
  save(request, reply) {

    let user;

    return new User({ email: request.payload.data.attributes.email })
      .fetch()
      .then(foundUser => {

        if (foundUser === null) {
          return Promise.reject();
        }

        user = foundUser;

        const givenPassword = request.payload.data.attributes.password;

        return encrypt.check(givenPassword, foundUser.get('password'));
      })
      .then(_ => {
        const token = jsonwebtoken.sign({
          user_id: user.get('id'),
          email: user.get('email')
        }, settings.authentication.secret, { expiresIn: settings.authentication.tokenLifespan });

        const authentication = new Authentication(user.get('id'), token);
        return reply(authenticationSerializer.serialize(authentication)).code(201);
      })
      .catch(() => {
        const message = validationErrorSerializer.serialize(_buildError());
        reply(message).code(400);
      });
  }
};

function _buildError() {
  return {
    data: {
      '': [ 'L\'adresse e-mail et/ou le mot de passe saisi(s) sont incorrects.' ]
    }
  };
}<commit_msg>[US-213] [FEATURE] A little improvement to make the code a little more human-readable<commit_after>const User = require('../../../lib/domain/models/data/user');

const jsonwebtoken = require('jsonwebtoken');

const encrypt = require('../../domain/services/encryption-service');
const validationErrorSerializer = require('../../infrastructure/serializers/jsonapi/validation-error-serializer');
const settings = require('../../settings');

const Authentication = require('../../domain/models/data/authentication');
const authenticationSerializer = require('../../infrastructure/serializers/jsonapi/authentication-serializer');

module.exports = {
  save(request, reply) {

    let user;
    const { password, email } = _extractAttributes(request);

    return new User({ email })
      .fetch()
      .then(foundUser => {

        if (foundUser === null) {
          return Promise.reject();
        }

        user = foundUser;
        return encrypt.check(password, foundUser.get('password'));
      })
      .then(_ => {
        const token = _createTokenFromUser(user);

        const authentication = new Authentication(user.get('id'), token);
        return reply(authenticationSerializer.serialize(authentication)).code(201);
      })
      .catch(() => {
        const message = validationErrorSerializer.serialize(_buildError());
        reply(message).code(400);
      });
  }
};

function _createTokenFromUser(user) {
  return jsonwebtoken.sign({
    user_id: user.get('id'),
    email: user.get('email')
  }, settings.authentication.secret, { expiresIn: settings.authentication.tokenLifespan });
}

function _extractAttributes(request) {
  return request.payload.data.attributes;
}

function _buildError() {
  return {
    data: {
      '': [ 'L\'adresse e-mail et/ou le mot de passe saisi(s) sont incorrects.' ]
    }
  };
}
{'fraction_non_alphanumeric': 0.15522643121617774, 'fraction_numerical': 0.004272287097692965, 'mean_word_length': 3.1909307875894988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1692736', 'n_tokens_mistral': 980, 'n_tokens_neox': 948, 'n_words': 186}