27 lines
820 B
JavaScript
27 lines
820 B
JavaScript
const bcrypt = require('bcrypt');
|
|
const saltRounds = 10;
|
|
|
|
class PasswordService {
|
|
/**
|
|
* Hashes the password in parameters.
|
|
* @param {String} password The plain text password.
|
|
* @returns The hashed password.
|
|
*/
|
|
hashPassword(password) {
|
|
const salt = bcrypt.genSaltSync(saltRounds);
|
|
return bcrypt.hashSync(password, salt);
|
|
}
|
|
|
|
/**
|
|
* Checks if the {@code plainTextPassword} matches the hashed password.
|
|
* @param {String} plainTextPassword The plain text password.
|
|
* @param {String} hashedPassword The hashed password.
|
|
* @returns A boolean.
|
|
*/
|
|
areSamePasswords(plainTextPassword, hashedPassword) {
|
|
return bcrypt.compareSync(plainTextPassword, hashedPassword);
|
|
}
|
|
}
|
|
|
|
const singleton = new PasswordService();
|
|
module.exports = singleton; |