<commit_before>var jwt = require('jsonwebtoken')
let models = require('../models')
let crypto = require('crypto');

/**
 * generates random string of characters i.e salt
 * @function
 * @param {number} length - Length of the random string.
 */
function genRandomString(length){
  return crypto.randomBytes(Math.ceil(length/2))
    .toString('hex') /** convert to hexadecimal format */
    .slice(0,length);   /** return required number of characters */
};

/**
 * hash password with sha512.
 * @function
 * @param {string} password - List of required fields.
 * @param {string} salt - Data to be validated.
 */
function hashPassword(password, salt){
  let hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */
  hash.update(password);
  let value = hash.digest('hex');
  return value;
};

function saltHashPassword(userpassword) {
  let salt = genRandomString(16); /** Gives us salt of length 16 */
  let hash = hashPassword(userpassword, salt);
  return {
    hash: hash,
    salt: salt
  }
}

module.exports = {
  // mendaftarkan pengguna baru
  register: function(req, res, next) {
    passwordData = saltHashPassword(req.body.password)
    var result = {
      success : false,
      status : "ERROR",
      user:{
        id: null,
        username: null,
        name: null,
        identityNumber: null,
        email: null,
        password : <PASSWORD>,
        updatedAt: null,
        createdAt : null,
        warehouseId: null
      }
    }
    models.User.create({
      name: req.body.name,
      email: req.body.email,
      username: req.body.username,
      identityNumber: req.body.identityNumber,
      password: <PASSWORD>,
      salt: passwordData.salt
    }).then(user => {
      result.success = true
      result.status = "OK"
      result.user.id = user.id
      result.user.username = user.username
      result.user.name = user.name
      result.user.identityNumber = user.identityNumber
      result.user.email = user.email
      result.user.password = <PASSWORD>
      result.user.updatedAt = user.updatedAt
      result.user.createdAt = user.createdAt
      result.user.warehouseId = user.warehouseId
      res.json(result)
    }).catch(err => {
      console.log('Error when trying to register : ', err);
      if (err.errors) {
        result.errors = err.errors
      }
      res.json(result)
    })
  },

  // masuk ke sistem sebagai suatu user
  login: function(req,res,next){
    var result = {
      success: false,
      status: "ERROR",
      message: "",
      user: {
        name: null,
        username: null,
        token: null
      }
    }
    models.User.findOne({
      where: {
        username: req.body.username
      },<commit_msg>hapus attirbut password pada respon endpoint create<commit_after>var jwt = require('jsonwebtoken')
let models = require('../models')
let crypto = require('crypto');

/**
 * generates random string of characters i.e salt
 * @function
 * @param {number} length - Length of the random string.
 */
function genRandomString(length){
  return crypto.randomBytes(Math.ceil(length/2))
    .toString('hex') /** convert to hexadecimal format */
    .slice(0,length);   /** return required number of characters */
};

/**
 * hash password with sha512.
 * @function
 * @param {string} password - List of required fields.
 * @param {string} salt - Data to be validated.
 */
function hashPassword(password, salt){
  let hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */
  hash.update(password);
  let value = hash.digest('hex');
  return value;
};

function saltHashPassword(userpassword) {
  let salt = genRandomString(16); /** Gives us salt of length 16 */
  let hash = hashPassword(userpassword, salt);
  return {
    hash: hash,
    salt: salt
  }
}

module.exports = {
  // mendaftarkan pengguna baru
  register: function(req, res, next) {
    passwordData = saltHashPassword(req.body.password)
    var result = {
      success : false,
      status : "ERROR",
      user:{
        id: null,
        username: null,
        name: null,
        identityNumber: null,
        email: null,
        updatedAt: null,
        createdAt : null,
        warehouseId: null
      }
    }
    models.User.create({
      name: req.body.name,
      email: req.body.email,
      username: req.body.username,
      identityNumber: req.body.identityNumber,
      password: <PASSWORD>,
      salt: passwordData.salt
    }).then(user => {
      result.success = true
      result.status = "OK"
      result.user.id = user.id
      result.user.username = user.username
      result.user.name = user.name
      result.user.identityNumber = user.identityNumber
      result.user.email = user.email
      result.user.updatedAt = user.updatedAt
      result.user.createdAt = user.createdAt
      result.user.warehouseId = user.warehouseId
      res.json(result)
    }).catch(err => {
      console.log('Error when trying to register : ', err);
      if (err.errors) {
        result.errors = err.errors
      }
      res.json(result)
    })
  },

  // masuk ke sistem sebagai suatu user
  login: function(req,res,next){
    var result = {
      success: false,
      status: "ERROR",
      message: "",
      user: {
        name: null,
        username: null,
        token: null
      }
    }
    models.User.findOne({
      where: {
        username: req.body.username
      },
{'fraction_non_alphanumeric': 0.12479077552538591, 'fraction_numerical': 0.0055793193230425885, 'mean_word_length': 2.471917366042608, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '2859104', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1600, 'n_words': 412}