I want to update a customer's information that consists of first name, last name, email and password. I want to encrypt the password when its being updated. I wrote my route like this:
customerRouter.put(
'/:id', isAuth,
expressAsyncHandler( async (req, res) => {
const {id} = req.params;
const customer = await Customers.findOne({where: {id: id}});
const {firstname,lastname,password,email} = req.body;
if (!customer) {
res.status(401).send({
message: "Customer Not Found",
});
}else{
bcrypt.hash(password,10).then((hash)=>{
customer.firstName = firstname || customer.firstName
customer.lastName = lastname || customer.lastName
customer.email = email || customer.email
customer.password = hash || customer.password
})
const updatedCustomer = await customer.save();
res.send({
id: updatedCustomer.id,
firstname: updatedCustomer.firstName,
lastname: updatedCustomer.lastName,
email: updatedCustomer.email,
isAdmin: updatedCustomer.isAdmin,
token: generateToken(updatedCustomer)
});
};
})
);
The password wont change in the backend, I had a previous method that didn't encrypt and only changed the password, but this one doesn't encrypt the password or change it. Any help/guidance is much appreciated.