import React from 'react'; import { Shield, ShieldAlert, ShieldCheck } from 'lucide-react'; interface PasswordStrengthMeterProps { password: string; } export function getPasswordStrengthDetails(password: string) { let score = 0; const checks = { length: password.length >= 8, uppercase: /[A-Z]/.test(password), lowercase: /[a-z]/.test(password), number: /[0-9]/.test(password), special: /[^A-Za-z0-9]/.test(password), longEnough: password.length >= 12, }; if (checks.length) score++; if (checks.uppercase) score++; if (checks.lowercase) score++; if (checks.number) score++; if (checks.special) score++; if (checks.longEnough) score++; let level: 'weak' | 'medium' | 'strong' | 'very-strong'; let label: string; let color: string; if (score <= 2) { level = 'weak'; label = 'ضعيفة'; color = 'hsl(var(--destructive))'; } else if (score <= 3) { level = 'medium'; label = 'متوسطة'; color = 'hsl(var(--warning))'; } else if (score <= 4) { level = 'strong'; label = 'قوية'; color = 'hsl(var(--primary))'; } else { level = 'very-strong'; label = 'قوية جداً'; color = 'hsl(var(--success))'; } return { score, level, label, color, checks, maxScore: 6 }; } const PasswordStrengthMeter: React.FC = ({ password }) => { if (!password) return null; const { score, label, color, checks, maxScore } = getPasswordStrengthDetails(password); const Icon = score <= 2 ? ShieldAlert : score <= 4 ? Shield : ShieldCheck; return (
{/* Strength bar */}
{Array.from({ length: maxScore }).map((_, i) => (
))}
{/* Label */}
{label}
{score}/{maxScore}
{/* Checklist */}
{[ { key: 'length', text: '8 أحرف' }, { key: 'uppercase', text: 'حرف كبير' }, { key: 'lowercase', text: 'حرف صغير' }, { key: 'number', text: 'رقم' }, { key: 'special', text: 'رمز خاص' }, { key: 'longEnough', text: '12+ حرف' }, ].map(({ key, text }) => ( {checks[key as keyof typeof checks] ? '✓' : '○'} {text} ))}
); }; export default PasswordStrengthMeter;