Просмотр исходного кода

Add validation to the creation of new merchants

Lee Morgan 6 лет назад
Родитель
Сommit
c0d48f613d

+ 5 - 4
controllers/merchantData.js

@@ -4,7 +4,7 @@ const bcrypt = require("bcryptjs");
 const Merchant = require("../models/merchant");
 const Recipe = require("../models/recipe");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
-const { update } = require("../models/merchant");
+const Validator = require("./validator.js");
 
 module.exports = {
     /*
@@ -18,9 +18,10 @@ module.exports = {
     Redirects to /dashboard
     */
     createMerchantNone: async function(req, res){
-        let merchant = await Merchant.findOne({email: req.body.email.toLowerCase()});
-        if(merchant){
-            req.session.error = "That email address is already in use";
+        let validation =  await Validator.merchant(req.body);
+        if(validation !== true){
+            console.log(validation);
+            req.session.error = validation;
             return res.redirect("/");
         }
 

+ 28 - 0
controllers/validator.js

@@ -1,4 +1,32 @@
+const Merchant = require("../models/merchant.js");
+
 module.exports = {
+    merchant: async function(merchant){
+        if(!this.isSanitary([merchant.name])){
+            return "Name contains illegal characters";
+        }
+
+        if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(merchant.email)){
+            return "Invalid email address";
+        }
+
+        let checkMerchant = await Merchant.findOne({email: merchant.email});
+
+        if(checkMerchant){
+            return "An account with that email address already exists";
+        }
+
+        if(merchant.password.length < 10){
+            return "Password must contain at least 10 characters";
+        }
+
+        if(merchant.password !== merchant.confirmPassword){
+            return "Passwords do not match";
+        }
+
+        return true;
+    },
+
     ingredient: function(ingredient){
         if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
             return false;

+ 2 - 0
views/landingPage/landing.ejs

@@ -107,6 +107,8 @@
 
         <% include ../shared/footer %>
 
+        <% include ../shared/loader %>
+
         <script>
             let error = <%- JSON.stringify(error) %>;
             let isLoggedIn = <%- JSON.stringify(isLoggedIn) %>;

+ 1 - 0
views/landingPage/register.js

@@ -38,6 +38,7 @@ let registerObj = {
 
         if(checkbox.checked){
             if(validator.isSanitary(document.querySelector("#regName").value)){
+                document.getElementById("loaderContainer").style.display = "flex";
                 form.action = "merchant/create/none";
                 form.method = "post";
                 form.submit();