import { createMemo, For, Show } from "solid-js"; import { validatePassword } from "~/lib/validation"; import { VALIDATION_CONFIG } from "~/config"; import CheckCircle from "./icons/CheckCircle"; interface PasswordStrengthMeterProps { password: string; showRequirements?: boolean; } interface Requirement { label: string; test: (password: string) => boolean; optional?: boolean; } export default function PasswordStrengthMeter( props: PasswordStrengthMeterProps ) { const validation = createMemo(() => validatePassword(props.password)); const strengthConfig = { weak: { color: "bg-red", textColor: "text-red", label: "Weak", width: "25%" }, fair: { color: "bg-yellow", textColor: "text-yellow", label: "Fair", width: "50%" }, good: { color: "bg-blue", textColor: "text-blue", label: "Good", width: "75%" }, strong: { color: "bg-green", textColor: "text-green", label: "Strong", width: "100%" } }; const requirements = createMemo(() => { const reqs: Requirement[] = [ { label: `At least ${VALIDATION_CONFIG.MIN_PASSWORD_LENGTH} characters`, test: (pwd) => pwd.length >= VALIDATION_CONFIG.MIN_PASSWORD_LENGTH } ]; if (VALIDATION_CONFIG.PASSWORD_REQUIRE_UPPERCASE) { reqs.push({ label: "One uppercase letter", test: (pwd) => /[A-Z]/.test(pwd) }); } if (VALIDATION_CONFIG.PASSWORD_REQUIRE_NUMBER) { reqs.push({ label: "One number", test: (pwd) => /[0-9]/.test(pwd) }); } reqs.push({ label: "One special character\n(recommended)", test: (pwd) => /[^A-Za-z0-9]/.test(pwd), optional: true }); return reqs; }); const strength = createMemo(() => validation().strength); const config = createMemo(() => strengthConfig[strength()]); return (