{"size": 1319, "ext": "js", "max_stars_repo_path": "api/src/resources/Auth/TokenAuth.js", "max_stars_repo_name": "cs-leonel-pelusi/retest-concrete", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/src/resources/Auth/TokenAuth.js", "max_issues_repo_name": "cs-leonel-pelusi/retest-concrete", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/src/resources/Auth/TokenAuth.js", "max_forks_repo_name": "cs-leonel-pelusi/retest-concrete", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.9818181818, "max_line_length": 74, "alphanum_fraction": 0.6383623958}
const hash = require('../../utils/hash');
const Boom = require('@hapi/boom');
const JWT = require('jsonwebtoken');

const { SECRET_KEY, ALGORITHM, EXPIRES_IN } = require('./config');
const { ERR_INVALID_TOKEN } = require('../../utils/errorTypes');
const { UserModel } = require('../../resources/User/UserModel');

const generateToken = async (payload) => {
  const { password, email } = payload;

  const user = await UserModel.findOne({ email });

  if (!user)
    throw Boom.notFound('User does not exists!');

  if (!await (hash.compare(password, user.password)))
    throw Boom.badData('Invalid Email or Password');

  const JWTData = {
    iss: 'csbe-api',
    sub: user.id,
    exp: Math.floor(Date.now() / 1000) + EXPIRES_IN,
  };

  try {
    const token = JWT.sign(JWTData, SECRET_KEY, { algorithm: ALGORITHM });

    return token;
  } catch (error) {
    throw new Error(ERR_INVALID_TOKEN);
  }
}

const generateTokeToStore = (payload) => {
  const { id } = payload;

  const JWTData = {
    iss: 'csbe-api',
    sub: id,
    exp: Math.floor(Date.now() / 1000) + EXPIRES_IN,
  };

  try {
    const token = JWT.sign(JWTData, SECRET_KEY, { algorithm: ALGORITHM });
    return token;
  } catch (error) {
    throw new Error(ERR_INVALID_TOKEN);
  }
}

module.exports = {
  generateToken,
  generateTokeToStore,
}