Parcourir la source

Add backend email uniqueness validation

Lee Morgan il y a 6 ans
Parent
commit
6d34fd21ab
1 fichiers modifiés avec 29 ajouts et 0 suppressions
  1. 29 0
      controllers/merchantData.js

+ 29 - 0
controllers/merchantData.js

@@ -141,6 +141,11 @@ 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;
@@ -223,6 +228,16 @@ module.exports = {
         let data = JSON.parse(req.body.data);
         data.email = data.email.toLowerCase();
 
+        if(data.password.length < 15 || data.password !== data.confirmPassword){
+            req.session.error = "Passwords must match and contain at least 15 characters";
+            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);
         
@@ -604,4 +619,18 @@ 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;
+    }
 }