fx-1 / src /components /auth /PasswordStrengthMeter.tsx
felix-fx's picture
Initial commit from GitHub fx-1
6711fd5 verified
Raw History Blame Contribute Delete
3.31 kB
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<PasswordStrengthMeterProps> = ({ password }) => {
if (!password) return null;
const { score, label, color, checks, maxScore } = getPasswordStrengthDetails(password);
const Icon = score <= 2 ? ShieldAlert : score <= 4 ? Shield : ShieldCheck;
return (
<div className="space-y-2 animate-in fade-in slide-in-from-top-2 duration-300">
{/* Strength bar */}
<div className="flex gap-1">
{Array.from({ length: maxScore }).map((_, i) => (
<div
key={i}
className="h-1.5 flex-1 rounded-full transition-all duration-300"
style={{
backgroundColor: i < score ? color : 'hsl(var(--muted))',
}}
/>
))}
</div>
{/* Label */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-1.5">
<Icon className="w-3 h-3" style={{ color }} />
<span className="text-[10px] font-bold" style={{ color }}>
{label}
</span>
</div>
<span className="text-[10px] text-muted-foreground">{score}/{maxScore}</span>
</div>
{/* Checklist */}
<div className="grid grid-cols-2 gap-1">
{[
{ key: 'length', text: '8 أحرف' },
{ key: 'uppercase', text: 'حرف كبير' },
{ key: 'lowercase', text: 'حرف صغير' },
{ key: 'number', text: 'رقم' },
{ key: 'special', text: 'رمز خاص' },
{ key: 'longEnough', text: '12+ حرف' },
].map(({ key, text }) => (
<span
key={key}
className={`text-[9px] font-medium ${
checks[key as keyof typeof checks]
? 'text-green-500'
: 'text-muted-foreground/50'
}`}
>
{checks[key as keyof typeof checks] ? '✓' : '○'} {text}
</span>
))}
</div>
</div>
);
};
export default PasswordStrengthMeter;