From 4186c9572e1dfdfd95c4a7029e269630003f6dd4 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 23 Sep 2024 23:44:21 +0530 Subject: [PATCH] feat: hash password --- server/src/utils/auth.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server/src/utils/auth.ts diff --git a/server/src/utils/auth.ts b/server/src/utils/auth.ts new file mode 100644 index 00000000..d46fad29 --- /dev/null +++ b/server/src/utils/auth.ts @@ -0,0 +1,24 @@ +import bcrypt from "bcrypt"; + +const hashPassword = (password) => { + return new Promise((resolve, reject) => { + bcrypt.genSalt(12, (err, salt) => { + if (err) { + reject(err) + } + bcrypt.hash(password, salt, (err, hash) => { + if (err) { + reject(err) + } + resolve(hash) + }) + }) + }) +} + +// password from frontend and hash from database +const comparePassword = (password, hash) => { + return bcrypt.compare(password, hash) +} + +module.exports = { hashPassword, comparePassword } \ No newline at end of file