diff --git a/app/composables/user_password_rules.js b/app/composables/user_password_rules.js new file mode 100644 index 0000000..cb9f7ac --- /dev/null +++ b/app/composables/user_password_rules.js @@ -0,0 +1,45 @@ +import { computed } from "vue"; + +export function useUserPasswordRules(passwordRef, firstnameRef, lastnameRef) { + const rules = { + minLength: 8, + alphanumeric: /^(?=.*[a-zA-Z])(?=.*\d)/, + specialChar: /[^a-zA-Z0-9]/, + }; + + const passwordErrors = computed(() => { + const errors = []; + + if (!passwordRef.value) { + return errors; + } + + if (passwordRef.value.length < rules.minLength) { + errors.push("Password must be at least 8 characters long"); + } + + if (!rules.alphanumeric.test(passwordRef.value)) { + errors.push("Password must contain letters and numbers"); + } + + if (!rules.specialChar.test(passwordRef.value)) { + errors.push("Password must contain at least one special character"); + } + + const first = firstnameRef?.value?.toLowerCase(); + const last = lastnameRef?.value?.toLowerCase(); + + if ( + (first && passwordRef.value.toLowerCase().includes(first)) || + (last && passwordRef.value.toLowerCase().includes(last)) + ) { + errors.push("Password must not contain your name"); + } + + return errors; + }); + + return { + passwordErrors, + }; +} \ No newline at end of file diff --git a/app/pages/account/register.vue b/app/pages/account/register.vue index 977ec2d..88d225b 100644 --- a/app/pages/account/register.vue +++ b/app/pages/account/register.vue @@ -1,6 +1,7 @@