]> Repos - mime-chat/commitdiff
add password strength check to register
authorEnrique Hernandez <hernandeze2@xavier.edu>
Sun, 28 Feb 2021 01:15:48 +0000 (19:15 -0600)
committerEnrique Hernandez <hernandeze2@xavier.edu>
Sun, 28 Feb 2021 01:15:48 +0000 (19:15 -0600)
src/views/register.vue

index 53193ec47e0418fdef8e638a1110b1ac7e9d31f5..7081ca8d11ae65be0391c64cc72216d72db594d5 100644 (file)
       <!-- submit button; position: fixed -->
       <button class="button" type="submit">Submit</button>
 
+      <div v-if="invalidEmail" class="invalid">Invalid Email.</div>
+      <div v-else-if="weakPass" class="invalid">Password must be 8-15 characters long.
+        <br>Password must have at least one:
+        <br>&emsp; Upper case letter
+        <br>&emsp; Lower case letter
+        <br>&emsp; One numeric digit
+        <br>&emsp; One special character
+      </div>
+      <div v-else-if="passUnMatch" class="invalid">Passwords dont match.</div>
+
     </form>
 
     <!-- back to login button: position: fixed  -->
@@ -45,49 +55,61 @@ interface RegisterPayload {
 export default defineComponent({
   name: "Register",
   setup() {
+    const router = useRouter();
+    const { invoke } = useIpc();
+    const { setToken } = useAuth();
+
     const email = ref("");
     const password = ref("");
     const password2 = ref("");
+    const invalidEmail = ref(false);
+    const weakPass = ref(false);
+    const passUnMatch = ref(false);
 
-    const { invoke } = useIpc();
-    const { setToken } = useAuth();
-    const router = useRouter();
+    // emai must be in '@email.com' format
+    const emailReg = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
+    // password must be 8-15 chars, have one upper, lower, number, and special char
+    const pwReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
 
-    const re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
+    // call this function on form submit.
+    const submit = async () => {
+        // reset invalid credentials references.
+        invalidEmail.value = false;
+        weakPass.value = false;
+        passUnMatch.value = false;
+
+        // validate email.
+        if (!emailReg.test(email.value)) {
+            invalidEmail.value = true;
+            return;
+        }
 
-    const passwordMatch = () => {
-        let match = false;
-        if (password.value == password2.value) {
-            match = true;
+        // validate password strength.
+        if (!pwReg.test(password.value)) {
+            weakPass.value = true;
+            return;
         }
-        return match;
-    }
 
-    const validate = () => {
-       let valid = false;
-       if (re.test(email.value) && passwordMatch()) {
-              valid = true;
-       }
-       return valid;
-    }
+        // check password match
+        if (password.value !== password2.value) {
+            passUnMatch.value = true;
+            return;
+        }
 
-    const submit = async () => {
+        const payload: RegisterPayload = {
+          request: "register",
+          email: email.value,
+          password: password.value,
+        };
+
+        try {
+            const res = await invoke('auth-session', payload);
+            setToken(res);
+            router.push({ name: "home" });
+        } catch(e) {
+            console.log('Failed to register.')
+        }
 
-      const payload: RegisterPayload = {
-        request: "register",
-        email: email.value,
-        password: password.value,
-      };
-
-      if (validate()) {
-          try {
-              const res = await invoke('auth-session', payload);
-              setToken(res);
-              router.push({ name: "home" });
-          } catch(e) {
-              console.log('Failed to register.')
-          }
-      }
     };
 
     const switchView = () => {
@@ -100,6 +122,9 @@ export default defineComponent({
       password2,
       switchView,
       submit,
+      invalidEmail,
+      weakPass,
+      passUnMatch,
     };
   },
 });
@@ -162,4 +187,11 @@ export default defineComponent({
   color: #58C4FD;
   font-weight: bold;
 }
+
+.invalid {
+    font-family: "SF Compact Display";
+    font-weight: bold;
+    font-size: 14px;
+    color: #F53737;
+}
 </style>