2020-01-14 22:22:42 +00:00
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
require('dotenv').config();
|
|
|
|
|
2020-01-16 04:04:55 +00:00
|
|
|
const saltRounds = process.env.NODE_ENV === 'test' ? 5 : parseInt(process.env.SALT_ROUNDS);
|
2020-01-14 22:22:42 +00:00
|
|
|
|
|
|
|
const hashPassword = async (password) => {
|
|
|
|
const hashedPassword = await new Promise((resolve, reject) => {
|
|
|
|
bcrypt.hash(password, saltRounds, (err, hash) => {
|
|
|
|
if (err) reject(err)
|
|
|
|
resolve(hash)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
return hashedPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
const compareHash = async (password, hash) => {
|
|
|
|
|
|
|
|
const success = await new Promise((resolve, reject) => {
|
|
|
|
bcrypt.compare(password, hash, (err, res) => {
|
|
|
|
if (err) reject(err);
|
2020-01-15 07:09:01 +00:00
|
|
|
if (res) resolve(true);
|
2020-01-14 22:22:42 +00:00
|
|
|
})
|
|
|
|
});
|
2020-01-15 07:09:01 +00:00
|
|
|
return success;
|
2020-01-14 22:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { hashPassword, compareHash };
|