Ver Fonte

Add frontend email validation. Backend email validation doesn't work.

Lee Morgan há 6 anos atrás
pai
commit
c9e2f2476e
4 ficheiros alterados com 46 adições e 32 exclusões
  1. 0 24
      controllers/merchantData.js
  2. 25 0
      controllers/otherData.js
  3. 1 0
      routes.js
  4. 20 8
      views/merchantSetupPage/basicInfo.js

+ 0 - 24
controllers/merchantData.js

@@ -141,11 +141,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        if(!this.isUniqueEmail){
-            req.session.error = "Email already in use";
-            return res.redirect("/");
-        }
-
         axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
             .then((cloverMerchant)=>{
                 req.session.posId = undefined;
@@ -233,11 +228,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        if(!this.isUniqueEmail){
-            req.session.error = "Email already in use";
-            return res.redirect("/");
-        }
-
         let salt = bcrypt.genSaltSync(10);
         let hash = bcrypt.hashSync(data.password, salt);
         
@@ -618,19 +608,5 @@ module.exports = {
 
                 return res.json(errorMessage);
             });
-    },
-
-    //Helper
-    isUniqueEmail: function(email){
-        Merchant.findOne({email: email})
-            .then((merchant)=>{
-                if(merchant){
-                    return false;
-                }else{
-                    return true;
-                }
-            });
-
-        return false;
     }
 }

+ 25 - 0
controllers/otherData.js

@@ -1,5 +1,6 @@
 const bcrypt = require("bcryptjs");
 
+const Error = require("../models/error");
 const NonPosTransaction = require("../models/nonPosTransaction");
 const Merchant = require("../models/merchant");
 
@@ -119,4 +120,28 @@ module.exports = {
 
         return res.redirect("/");
     },
+
+    //POST - check an email for uniqueness
+    //
+    checkUniqueEmail: function(req, res){
+        Merchant.findOne({email: req.body.email})
+            .then((merchant)=>{
+                if(merchant){
+                    return res.json(false);
+                }else{
+                    return res.json(true);
+                }
+            })
+            .catch((err)=>{
+                let errorMessage = "Error: unable to validate email address";
+                let error = new Error({
+                    code: 626,
+                    displayMessage: errorMessage,
+                    error: err
+                });
+                error.save();
+
+                return res.json(errorMessage);
+            });
+    }
 }

+ 1 - 0
routes.js

@@ -31,4 +31,5 @@ module.exports = function(app){
     app.post("/transactions/create", otherData.createTransaction);  //Creates transaction for non-pos merchant
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
+    app.post("/email", otherData.checkUniqueEmail);
 }

+ 20 - 8
views/merchantSetupPage/basicInfo.js

@@ -16,13 +16,25 @@ basicInfoObj = {
         let password = document.querySelector("#regPass").value;
         let confirmPassword = document.querySelector("#regConfirmPass").value;
 
-        if(validator.merchant.password(password, confirmPassword)){
-            controller.data.name = name;
-            controller.data.email = email;
-            controller.data.password = password;
-            controller.data.confirmPassword = confirmPassword;
-
-            addIngredientsObj.display();
-        }
+        axios.post("/email", {email: email})
+            .then((response)=>{
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
+                }else if(response.data){
+                    if(validator.merchant.password(password, confirmPassword)){
+                        controller.data.name = name;
+                        controller.data.email = email;
+                        controller.data.password = password;
+                        controller.data.confirmPassword = confirmPassword;
+            
+                        addIngredientsObj.display();
+                    }
+                }else{
+                    banner.createError("Email address already in use");
+                }
+            })
+            .catch((err)=>{
+                banner.createError("Error: unable to validate email address");
+            });
     }
 }