Parcourir la source

Add validation to reset password

Lee Morgan il y a 6 ans
Parent
commit
47ce5bfc4b

+ 32 - 26
controllers/merchantData.js

@@ -171,6 +171,10 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let validation = Validate.quantity(req.body.quantity);
+        if(validation !== true){
+            return res.json(validation);
+        }
 
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
@@ -242,6 +246,11 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let validation = Validator.quantity(req.body.quantity);
+        if(validation !== true){
+            return res.json(validation);
+        }
+
         let adjustments = [];
 
         Merchant.findOne({_id: req.session.user})
@@ -282,40 +291,37 @@ module.exports = {
     },
 
     /*
-    //POST - Update merchant password
+    POST - Changes the users password
     req.body = {
-        oldPass: current merchant password (supposedly),
-        newPass: replacement password
+        pass: new password,
+        confirmPass: new password confirmation,
+        hash: hashed version of old password
     }
     */
     updatePassword: function(req, res){
-        if(!req.session.user){
-            req.session.error = "Must be logged in to do that";
-            return res.redirect("/");
+        let validation = Validator.password(req.body.pass, req.body.confirmPass);
+        if(validation !== true){
+            return res.json(validation);
         }
 
-        Merchant.findOne({_id: req.session.user})
+        Merchant.findOne({password: req.body.hash})
             .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);
+                if(merchant){
+                    let salt = bcrypt.genSaltSync(10);
+                    let hash = bcrypt.hashSync(req.body.pass, 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");
-                    }
-                });
+                    merchant.password = hash;
+
+                    return merchant.save();
+                }else{
+                    req.session.error = "Error: unable to retrieve merchant data";
+                    return res.redirect("/");
+                }
             })
-            .catch((err)=>{
-                return res.json("Error: Unable to retrieve merchant data");
-            });
+            .then((merchant)=>{
+                req.session.error = "Password successfully reset.  Please log in";
+                return res.redirect("/");
+            })
+            .catch((err)=>{});
     }
 }

+ 0 - 32
controllers/otherData.js

@@ -2,8 +2,6 @@ const bcrypt = require("bcryptjs");
 const axios = require("axios");
 
 const Merchant = require("../models/merchant");
-const Order = require("../models/order");
-const Transaction = require("../models/transaction");
 
 module.exports = {
     /*
@@ -99,35 +97,5 @@ module.exports = {
                 req.session.error = "Error: Unable to retrieve data from Clover";
                 return res.redirect("/");
             });
-    },
-
-    /*
-    POST - Changes the users password
-    req.body = {
-        pass: new password,
-        confirmPass: new password confirmation,
-        hash: hashed version of old password
-    }
-    */
-    resetPassword: function(req, res){
-        Merchant.findOne({password: req.body.hash})
-            .then((merchant)=>{
-                if(merchant && req.body.pass === req.body.confirmPass){
-                    let salt = bcrypt.genSaltSync(10);
-                    let hash = bcrypt.hashSync(req.body.pass, salt);
-
-                    merchant.password = hash;
-
-                    return merchant.save();
-                }else{
-                    req.session.error = "Error: unable to retrieve merchant data";
-                    return res.redirect("/");
-                }
-            })
-            .then((merchant)=>{
-                req.session.error = "Password successfully reset.  Please log in";
-                return res.redirect("/");
-            })
-            .catch((err)=>{});
     }
 } 

+ 0 - 1
controllers/renderer.js

@@ -3,7 +3,6 @@ const ObjectId = require("mongoose").Types.ObjectId;
 
 const Merchant = require("../models/merchant");
 const Transaction = require("../models/transaction");
-const Order = require("../models/order");
 
 module.exports = {
     /*

+ 11 - 2
controllers/validator.js

@@ -16,11 +16,20 @@ module.exports = {
             return "An account with that email address already exists";
         }
 
-        if(merchant.password.length < 10){
+        let checkPassword = this.password(merchant.password, merchant.confirmPassword);
+        if(this.password(checkPassword !== true)){
+            return checkPassword;
+        }
+
+        return true;
+    },
+
+    password: function(password, confirmPassword){
+        if(password.length < 10){
             return "Password must contain at least 10 characters";
         }
 
-        if(merchant.password !== merchant.confirmPassword){
+        if(password !== confirmPassword){
             return "Passwords do not match";
         }
 

+ 0 - 1
routes.js

@@ -40,7 +40,6 @@ module.exports = function(app){
     app.get("/logout", otherData.logout);
     app.get("/cloverlogin", otherData.cloverRedirect);
     app.get("/cloverauth*", otherData.cloverAuth);
-    app.post("/resetpassword", otherData.resetPassword);
 
     //Transactions
     app.get("/populatesometransactions", transactionData.populate);

+ 0 - 1
views/dashboardPage/components/components.js

@@ -361,7 +361,6 @@ let newIngredientComp = {
                     }
                 })
                 .catch((err)=>{
-                    console.log(err);
                     banner.createError("Something went wrong.  Try refreshing the page");
                 })
                 .finally(()=>{

+ 1 - 1
views/passResetPage/passReset.ejs

@@ -15,7 +15,7 @@
         <div class="content">
             <h1>Reset Password</h1>
 
-            <form action="/resetpassword" method="post" onsubmit="submitPass()">
+            <form action="/merchant/password" method="post" onsubmit="submitPass()">
                 <label>New Password:
                     <input type="password" id="pass" name="pass" required>
                 </label>