Skip to content

Commit 6035eb9

Browse files
committed
feat(ui): add PasswordStrength indicator
1 parent 02a9ae3 commit 6035eb9

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
interface Props { password: string; }
3+
const getStrength = (p: string): { score: number; label: string; color: string } => {
4+
let score = 0;
5+
if (p.length >= 8) score++;
6+
if (/[a-z]/.test(p) && /[A-Z]/.test(p)) score++;
7+
if (/\d/.test(p)) score++;
8+
if (/[^a-zA-Z0-9]/.test(p)) score++;
9+
if (p.length >= 12) score++;
10+
const labels = ['Very Weak', 'Weak', 'Fair', 'Strong', 'Very Strong'];
11+
const colors = ['bg-red-500', 'bg-orange-500', 'bg-yellow-500', 'bg-green-500', 'bg-emerald-500'];
12+
const idx = Math.min(score, 4);
13+
return { score, label: labels[idx], color: colors[idx] };
14+
};
15+
const PasswordStrength: React.FC<Props> = ({ password }) => {
16+
if (!password) return null;
17+
const { score, label, color } = getStrength(password);
18+
return (
19+
<div className='mt-2'>
20+
<div className='flex gap-1 mb-1'>{Array(5).fill(0).map((_, i) => (<div key={i} className={'h-1 flex-1 rounded-full ' + (i <= score - 1 ? color : 'bg-gray-200')} />))}</div>
21+
<p className='text-xs text-gray-500'>{label}</p>
22+
</div>
23+
);
24+
};
25+
export default PasswordStrength;

0 commit comments

Comments
 (0)