Переглянути джерело

Create ability to change account information. Create ability to change password.

Lee Morgan 6 роки тому
батько
коміт
ac17c570de

+ 61 - 1
controllers/merchantData.js

@@ -693,7 +693,67 @@ module.exports = {
             });
     },
 
+    //POST - Update merchant information
+    //Inputs:
+    //  req.body.name: name update
+    //  req.body.email: email update
+    //Returns: Nothing
     updateMerchant: function(req, res){
-        console.log("Something or other");
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                merchant.name = req.body.name;
+                merchant.email = req.body.email;
+                merchant.save()
+                    .then((updatedMerchant)=>{
+                        return res.json({});
+                    })
+                    .catch((err)=>{
+                        return res.json("Error: unable to save merchant data");
+                    });
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve merchant data");
+            });
+    },
+
+    //POST - Update merchant password
+    //Inputs:
+    //  req.body.oldPass:  current merchant password (supposedly)
+    //  req.body.newPass:  replacement password
+    //Returns: Nothing
+    updatePassword: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                bcrypt.compare(req.body.oldPass, merchant.password, (err, result)=>{
+                    if(result){
+                        let salt = bcrypt.genSaltSync(10);
+                        let hash = bcrypt.hashSync(req.body.newPass, salt);
+
+                        merchant.password = hash;
+                        merchant.save()
+                            .then((updatedMerchant)=>{
+                                return res.json({});
+                            })
+                            .catch((err)=>{
+                                return res.json("Error: Unable to save new password");
+                            });
+                    }else{
+                        return res.json("Error: old password does not match current password");
+                    }
+                });
+            })
+            .catch((err)=>{
+                return res.json("Error: Unable to retrieve merchant data");
+            });
     }
 }

+ 1 - 0
routes.js

@@ -23,6 +23,7 @@ module.exports = function(app){
     app.post("/merchant/recipes/ingredients/update", merchantData.updateRecipeIngredient);
     app.post("/merchant/recipes/ingredients/remove", merchantData.removeRecipeIngredient);
     app.post("/merchant/update", merchantData.updateMerchant);
+    app.post("/merchant/password", merchantData.updatePassword);
 
     //Ingredients
     app.get("/ingredients", ingredientData.getIngredients);

+ 44 - 2
views/inventoryPage/account.js

@@ -31,7 +31,7 @@ window.accountObj = {
                         document.querySelector("#accountDisplay").style.display = "flex";
                         document.querySelector("#accountEdit").style.display = "none";
 
-                        let labels = document.querySelector("#accountDisplay label");
+                        let labels = document.querySelectorAll("#accountDisplay label");
                         labels[0].children[0].innerText = merchant.name;
                         labels[1].children[0].innerText = merchant.email;
                     }
@@ -42,11 +42,53 @@ window.accountObj = {
         }
     },
 
-    editPassword: function(){
+    editAccountCancel: function(){
+        event.preventDefault();
+
+        document.querySelector("#accountDisplay").style.display = "flex";
+        document.querySelector("#accountEdit").style.display = "none";
+    },
 
+    editPassword: function(){
+        document.querySelector("#passwordEdit").style.display = "flex";
+        document.querySelector("#accountStrand > button").style.display = "none";
     },
 
     updatePassword: function(){
+        event.preventDefault();
+
+        let oldPass = document.querySelector("#oldPass").value;
+        let newPass = document.querySelector("#newPass").value;
+        let confirmNewPass = document.querySelector("#confirmNewPass").value;
+
+        if(validator.merchant.password(newPass, confirmNewPass)){
+            axios.post("/merchant/password", {oldPass: oldPass, newPass: newPass})
+                .then((response)=>{
+                    if(typeof(response.data) === "string"){
+                        banner.createError(response.data);
+                    }
+
+                    document.querySelector("#oldPass").value = "";
+                    document.querySelector("#newPass").value = "";
+                    document.querySelector("#confirmNewPass").value = "";
+
+                    document.querySelector("#passwordEdit").style.display = "none";
+                    document.querySelector("#accountStrand > button").style.display = "block";
+                })
+                .catch((err)=>{
+                    banner.createError("Error: please refresh page to check for updates");
+                });
+        }
+    },
+
+    editPasswordCancel: function(){
+        event.preventDefault();
+
+        document.querySelector("#oldPass").value = "";
+        document.querySelector("#newPass").value = "";
+        document.querySelector("#confirmNewPass").value = "";
 
+        document.querySelector("#passwordEdit").style.display = "none";
+        document.querySelector("#accountStrand > button").style.display = "block";
     }
 }

+ 4 - 0
views/inventoryPage/inventory.css

@@ -91,6 +91,10 @@
         display: none;
     }
 
+    #passwordEdit{
+        display: none;
+    }
+
 /* Add Ingredient Action */
 #addIngredientAction{
     flex-direction: column;

+ 28 - 3
views/inventoryPage/inventory.ejs

@@ -66,7 +66,7 @@
                     <p><%=merchant.email%></p>
                 </label>
 
-                <button class="button">Edit</button>
+                <button class="button" onclick="accountObj.editAccount()">Edit</button>
             </form>
 
             <form id="accountEdit" onsubmit="accountObj.updateAccount()">
@@ -78,10 +78,35 @@
                     <input id="accountEmail" type="email" value="<%=merchant.email%>">
                 </label>
 
-                <button class="button">Save</button>
+                <div class="buttonBox">
+                    <button class="button" onclick="accountObj.updateAccount()">Save</button>
+
+                    <button class="button" onclick="accountObj.editAccountCancel()">Cancel</button>
+                </div>
+            </form>
+
+            <form id="passwordEdit" onsubmit="accountObj.updatePassword()">
+                <label>Old password:
+                    <input id="oldPass" type="password" required>
+                </label>
+
+                <label>New password:
+                    <input id="newPass" type="password" required>
+                </label>
+
+                <label>Confirm new password:
+                    <input id="confirmNewPass" type="password" required>
+                </label>
+
+
+                <div class="buttonBox">
+                    <input class="button" type="submit" value="Submit">
+
+                    <button class="button" onclick="accountObj.editPasswordCancel()">Cancel</button>
+                </div>
             </form>
 
-            <button class="button" onclick="accountObj.passChangeDisplay()">Change password</button>
+            <button class="button" onclick="accountObj.editPassword()">Change password</button>
         </div>
 
 

+ 5 - 0
views/shared/shared.css

@@ -88,6 +88,11 @@ form{
     transition: 0.3s; 
 }
 
+.buttonBox{
+    display: flex;
+    justify-content: space-around;
+}
+
     .button-small:hover{
         background: rgb(0, 27, 45);
         color: rgb(240, 252, 255);