Skip to content

Commit a9e96ae

Browse files
authored
Merge pull request #69 from Yeom-JinHo/fix/password-confirm
fix: revalidate confirmPassword field on password change
2 parents 1aad864 + bb91104 commit a9e96ae

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

apps/ui-layout/registry/components/form/password/confirm-pass-check.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
HoverCardContent,
88
HoverCardTrigger,
99
} from '@/components/website/ui/hover-card';
10-
import { boolean } from 'zod';
1110

1211
// Constants
1312
const PASSWORD_REQUIREMENTS = [
@@ -51,7 +50,7 @@ type PasswordStrength = {
5150

5251
const PasswordInput = () => {
5352
const [password, setPassword] = useState('');
54-
const [confirmPassword, setConfirmPassword] = useState<null | boolean>(null);
53+
const [confirmPassword, setConfirmPassword] = useState('');
5554
const [isVisible, setIsVisible] = useState(false);
5655

5756
const calculateStrength = useMemo((): PasswordStrength => {
@@ -66,10 +65,11 @@ const PasswordInput = () => {
6665
};
6766
}, [password]);
6867

69-
const handleConfirmpass = (e: React.ChangeEvent<HTMLInputElement>) => {
70-
const passwordsMatch = password !== '' && e.target.value === password;
71-
setConfirmPassword(passwordsMatch);
72-
};
68+
const isMatch = useMemo(() => {
69+
if (confirmPassword === '') return null;
70+
return password !== '' && confirmPassword === password;
71+
}, [password, confirmPassword]);
72+
7373
return (
7474
<div className='w-96 mx-auto py-12'>
7575
<form className='space-y-4'>
@@ -146,18 +146,19 @@ const PasswordInput = () => {
146146
<input
147147
id='confirm-password'
148148
type={isVisible ? 'text' : 'password'}
149-
onChange={(e) => handleConfirmpass(e)}
149+
value={confirmPassword}
150+
onChange={(e) => setConfirmPassword(e.target.value)}
150151
placeholder='Confirm Password'
151-
aria-invalid={!confirmPassword}
152+
aria-invalid={confirmPassword !== '' ? isMatch === false : undefined}
152153
className={`cursor-pointer w-full p-2 border-2 rounded-md dark:bg-neutral-800 bg-neutral-50 outline-none transition-all ${
153-
confirmPassword == null
154+
confirmPassword === ''
154155
? ''
155-
: confirmPassword
156+
: isMatch
156157
? 'border-green-400'
157158
: 'border-red-500'
158159
}`}
159160
/>
160-
{!confirmPassword && confirmPassword && (
161+
{confirmPassword !== '' && isMatch === false && (
161162
<p className='text-red-500 text-sm mt-1'>Passwords do not match</p>
162163
)}
163164
</div>

0 commit comments

Comments
 (0)