/* * User utilities */ const bcrypt = require(`bcryptjs`); const jwt = require(`jsonwebtoken`); const getToken = (user) => { return jwt.sign( convertToPublicFormat(user), process.env.tokenSecret, { expiresIn: 604800, // 1 week } ); }; const convertToPublicFormat = (user = {}) => { user.email = user.email || user.hk || null; user.id = user.id || user.sk2 || null; if (user.hk) { delete user.hk; } if (user.sk) { delete user.sk; } if (user.sk2) { delete user.sk2; } if (user.password) { delete user.password; } return user; }; /** * Hash password * @param {string} password Password to hash */ const hashPassword = (password) => { const salt = bcrypt.genSaltSync(10); return bcrypt.hashSync(password, salt); }; /** * Compare password * @param {string} candidatePassword Hashed password supplied by user * @param {string} trustedPassword Hashed password on record for user */ const comparePassword = (candidatePassword, trustedPassword) => { return bcrypt.compareSync(candidatePassword, trustedPassword); }; module.exports = { getToken, convertToPublicFormat, hashPassword, comparePassword, }