Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bcrypt hashing #19

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.kSaltRounds = 10;
22 changes: 7 additions & 15 deletions controllers/userAuth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const User = require("../models/user.model");
const UserCredentials = require("../models/user.credentials");
const OTP = require("../models/otp.model");
const nodemailer = require("nodemailer");
const bcrypt = require("bcrypt");
const {kSaltRounds} = require("../constants");

const transporter = nodemailer.createTransport({
port: 465,
Expand Down Expand Up @@ -93,10 +95,12 @@ exports.createNewUser = async (req, res, next) => {
// save user
const user = await createUser.save();

const hashedPassword = await bcrypt.hash(password,kSaltRounds);

const createUserCredentials = new UserCredentials({
user_id: user._id,
email,
password,
password: hashedPassword,
});

createUserCredentials.save();
Expand All @@ -109,18 +113,6 @@ exports.createNewUser = async (req, res, next) => {
entityModel: "User",
});

// await new Promise((resolve, reject) => {
// // verify connection configuration
// transporter.verify(function (error, success) {
// if (error) {
// console.log(error);
// reject(error);
// } else {
// console.log("Server is ready to take our messages");
// resolve(success);
// }
// });
// });

let mailData = {
from: {
Expand All @@ -132,7 +124,7 @@ exports.createNewUser = async (req, res, next) => {
text: `Your Otp is - ${otp}`,
};

await new Promise((resolve, reject) => {
new Promise((resolve, reject) => {
// send mail
transporter.sendMail(mailData, (err, info) => {
if (err) {
Expand Down Expand Up @@ -169,7 +161,7 @@ exports.login = async (req, res, next) => {
next({ status: 401, message: USER_NOT_VERIFIED });
}

const passwordMatch = password === user.password ? 1 : 0;
const passwordMatch = await bcrypt.compare(password, user.password);

if (passwordMatch) {
const token = createJwtToken({ userId: user.user_id });
Expand Down
63 changes: 33 additions & 30 deletions controllers/vendorAuth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const OTP = require("../models/otp.model");

const nodemailer = require("nodemailer");
const bcrypt = require("bcrypt");
const {kSaltRounds} = require('../constants')

const {
USER_NOT_FOUND_ERR,
Expand All @@ -22,7 +23,7 @@ let mailTransporter = nodemailer.createTransport({
pass: "mxzc acbf revb xcxh",
});

// --------------------- create new user ---------------------------------
// --------------------- create new Vendor ---------------------------------

exports.createNewVendor = async (req, res, next) => {
try {
Expand All @@ -42,14 +43,16 @@ exports.createNewVendor = async (req, res, next) => {

console.log(description);

// let images = [image_url];


const emailExist = await Vendor.findOne({ email });
if (emailExist) {
next({ status: 400, message: EMAIL_ALREADY_EXISTS_ERR });
return;
}

const hashedPassword = await bcrypt.hash(password, kSaltRounds);

const createVendor = new Vendor({
ownerName,
email,
Expand All @@ -66,7 +69,7 @@ exports.createNewVendor = async (req, res, next) => {

const createVendorCredentials = new VendorCredentials({
email,
password,
password: hashedPassword,
vendor_id: vendor._id,
});
await createVendorCredentials.save();
Expand All @@ -77,32 +80,32 @@ exports.createNewVendor = async (req, res, next) => {
});
await menu.save();

const otp = Math.floor(1000 + Math.random() * 9000);
const sentOtp = new OTP({
code: otp,
expiresAt: new Date(new Date().getTime() + 2 * 60 * 1000),
entity: vendor._id,
entityModel: "Vendor",
});
await sentOtp.save();

let mailDetails = {
from: "[email protected]",
to: email,
subject: "Test mail",
text: `Your OTP is: ${otp}`,
};

mailTransporter.sendMail(mailDetails, function (err, data) {
if (err) {
console.log("Error Occurs");
console.log(err);
} else {
console.log("Email sent successfully");
}
});

res.status(200).json("OTP send successfully");
// const otp = Math.floor(1000 + Math.random() * 9000);
// const sentOtp = new OTP({
// code: otp,
// expiresAt: new Date(new Date().getTime() + 2 * 60 * 1000),
// entity: vendor._id,
// entityModel: "Vendor",
// });
// await sentOtp.save();

// let mailDetails = {
// from: "[email protected]",
// to: email,
// subject: "Test mail",
// text: `Your OTP is: ${otp}`,
// };c

// mailTransporter.sendMail(mailDetails, function (err, data) {
// if (err) {
// console.log("Error Occurs");
// console.log(err);
// } else {
// console.log("Email sent successfully");
// }
// });

res.status(200).json("Register successfully");
} catch (error) {
next(error);
}
Expand All @@ -120,7 +123,7 @@ exports.vendorLogin = async (req, res, next) => {
return;
}

const passwordMatch = vendor.password === password;
const passwordMatch = await bcrypt.compare(password, vendor.password);
if (passwordMatch) {
// Generate JWT token
const token = createJwtToken({ userId: vendor.vendor_id });
Expand Down