add password helpers
This commit is contained in:
parent
2b9dbd0e83
commit
410c2e1cc5
41
src/utils/helpers/passwordHelpers.js
Normal file
41
src/utils/helpers/passwordHelpers.js
Normal file
@ -0,0 +1,41 @@
|
||||
import bcrypt from "bcrypt";
|
||||
|
||||
/**
|
||||
* Generates a random salt for a password
|
||||
*
|
||||
* @return The random salt
|
||||
*/
|
||||
export function generateSalt() {
|
||||
return randomString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random password
|
||||
*
|
||||
* @return The password
|
||||
*/
|
||||
export function generateRandomPassword() {
|
||||
return randomString(8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes the password ready for use in the database
|
||||
*
|
||||
* @param {string} salt The salt
|
||||
* @param {string} password The password that the user gave
|
||||
* @return The hashed password
|
||||
*/
|
||||
export function hashPassword(salt, password) {
|
||||
return bcrypt.hashSync(password, salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the password is valid with the salt
|
||||
*
|
||||
* @param {string} salt The salt
|
||||
* @param {string} password The password that the user gave
|
||||
* @return If the password is valid or not
|
||||
*/
|
||||
export function isValidPassword(salt, password) {
|
||||
return bcrypt.compareSync(password, salt);
|
||||
}
|
Reference in New Issue
Block a user