nuxt-users
Version:
A comprehensive user management module for Nuxt 3 and Nuxt 4 applications with authentication, authorization, database support, and CLI tools
38 lines (37 loc) • 1.45 kB
JavaScript
import { ref, computed } from "vue";
import { validatePassword, getPasswordValidationOptions, getPasswordStrengthColor, getPasswordStrengthText } from "../../utils.js";
export const usePasswordValidation = (moduleOptions, options) => {
const passwordOptions = getPasswordValidationOptions(moduleOptions);
const finalOptions = { ...passwordOptions, ...options };
const password = ref("");
const validationResult = ref(null);
const validate = (passwordToValidate) => {
password.value = passwordToValidate;
validationResult.value = validatePassword(passwordToValidate, finalOptions);
return validationResult.value;
};
const isValid = computed(() => validationResult.value?.isValid ?? false);
const errors = computed(() => validationResult.value?.errors ?? []);
const hints = computed(() => validationResult.value?.hints ?? []);
const strength = computed(() => validationResult.value?.strength ?? "weak");
const score = computed(() => validationResult.value?.score ?? 0);
const strengthColor = computed(() => getPasswordStrengthColor(strength.value));
const strengthText = computed(() => getPasswordStrengthText(strength.value));
const clearValidation = () => {
password.value = "";
validationResult.value = null;
};
return {
password,
validationResult,
validate,
isValid,
errors,
hints,
strength,
score,
strengthColor,
strengthText,
clearValidation
};
};