nuxt-users
Version:
A comprehensive user management module for Nuxt 3 and Nuxt 4 applications with authentication, authorization, database support, and CLI tools
163 lines (161 loc) • 4.2 kB
JavaScript
const DEFAULT_OPTIONS = {
minLength: 8,
requireUppercase: true,
requireLowercase: true,
requireNumbers: true,
requireSpecialChars: true,
preventCommonPasswords: true
};
const getPasswordValidationOptions = (moduleOptions) => {
if (!moduleOptions?.passwordValidation) {
return DEFAULT_OPTIONS;
}
return {
...DEFAULT_OPTIONS,
...moduleOptions.passwordValidation
};
};
const COMMON_PASSWORDS = [
"password",
"123456",
"123456789",
"qwerty",
"abc123",
"password123",
"admin",
"letmein",
"welcome",
"monkey",
"dragon",
"master",
"hello",
"freedom",
"whatever",
"qazwsx",
"trustno1",
"jordan",
"harley",
"ranger",
"buster",
"thomas",
"tigger",
"robert",
"soccer",
"batman",
"test",
"pass",
"user",
"guest",
"login",
"secret",
"god",
"love",
"sex",
"money",
"password1",
"12345678",
"qwerty123",
"admin123",
"password!",
"password1!",
"password123",
"password123!"
];
const validatePassword = (password, options = {}) => {
const config = { ...DEFAULT_OPTIONS, ...options };
const errors = [];
const hints = [];
let score = 0;
if (password.length < config.minLength) {
errors.push(`Password must be at least ${config.minLength} characters long`);
hints.push(`Use at least ${config.minLength} characters`);
} else {
score += 20;
if (password.length < 12) {
hints.push("Use 12 or more characters for extra security");
}
}
if (config.requireUppercase && !/[A-Z]/.test(password)) {
errors.push("Password must contain at least one uppercase letter");
hints.push("Add an uppercase letter (A-Z)");
} else if (/[A-Z]/.test(password)) {
score += 15;
}
if (config.requireLowercase && !/[a-z]/.test(password)) {
errors.push("Password must contain at least one lowercase letter");
hints.push("Add a lowercase letter (a-z)");
} else if (/[a-z]/.test(password)) {
score += 15;
}
if (config.requireNumbers && !/\d/.test(password)) {
errors.push("Password must contain at least one number");
hints.push("Add a number (0-9)");
} else if (/\d/.test(password)) {
score += 15;
}
if (config.requireSpecialChars && !/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(password)) {
errors.push("Password must contain at least one special character");
hints.push("Add a special character (e.g. !@#$%)");
} else if (/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(password)) {
score += 15;
}
if (config.preventCommonPasswords && COMMON_PASSWORDS.includes(password.toLowerCase())) {
errors.push("Password is too common. Please choose a more unique password");
hints.push("Avoid common passwords or names");
}
if (password.length >= 12) {
score += 10;
} else if (password.length >= 10) {
score += 5;
}
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasNumbers = /\d/.test(password);
const hasSpecial = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(password);
const complexityTypes = [hasUppercase, hasLowercase, hasNumbers, hasSpecial].filter(Boolean).length;
if (complexityTypes >= 4) {
score += 10;
} else if (complexityTypes >= 3) {
score += 5;
hints.push("Use a mix of uppercase, lowercase, numbers, and special characters");
}
let strength = "weak";
if (score >= 80) {
strength = "strong";
} else if (score >= 60) {
strength = "medium";
}
return {
isValid: errors.length === 0,
errors,
strength,
score: Math.min(score, 100),
hints: Array.from(new Set(hints))
// deduplicate
};
};
const getPasswordStrengthColor = (strength) => {
switch (strength) {
case "weak":
return "#dc3545";
case "medium":
return "#ffc107";
case "strong":
return "#28a745";
default:
return "#6c757d";
}
};
const getPasswordStrengthText = (strength) => {
switch (strength) {
case "weak":
return "Weak";
case "medium":
return "Medium";
case "strong":
return "Strong";
default:
return "Unknown";
}
};
export { getPasswordStrengthColor, getPasswordStrengthText, getPasswordValidationOptions, validatePassword };