From 410c2e1cc5e063670ecee92bc506fa9d3f5a5a9e Mon Sep 17 00:00:00 2001 From: Liam <67254223+RealFascinated@users.noreply.github.com> Date: Sun, 13 Nov 2022 20:20:00 +0000 Subject: [PATCH] add password helpers --- src/utils/helpers/passwordHelpers.js | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/utils/helpers/passwordHelpers.js diff --git a/src/utils/helpers/passwordHelpers.js b/src/utils/helpers/passwordHelpers.js new file mode 100644 index 0000000..49471fb --- /dev/null +++ b/src/utils/helpers/passwordHelpers.js @@ -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); +}