Skip to content

Commit 10b2e64

Browse files
Merge pull request #71 from cheehongw/password-strength
Password strength
2 parents c1b6049 + 9c3c628 commit 10b2e64

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

frontend/src/components/auth/LoginForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function LoginForm() {
5757

5858
<FormControl id='password' isRequired>
5959
<FormLabel>Password</FormLabel>
60-
<Input type='text'
60+
<Input type='password'
6161
name="password"
6262
value={password}
6363
onChange={(e) => { setPassword(e.target.value) }}

frontend/src/components/auth/ResgistrationForm.tsx

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,61 @@ import { setUser } from '../../reducers/authSlice';
1414
import { useDispatch } from 'react-redux';
1515
import { useNavigate } from 'react-router-dom';
1616
import { register } from '../../api/auth';
17+
import { AxiosError } from 'axios';
1718

1819
export default function RegistrationForm() {
1920
const [username, setUsername] = useState('');
2021
const [password, setPassword] = useState('');
22+
const [rePassword, setRePassword] = useState('');
2123
const dispatch = useDispatch()
2224
const navigate = useNavigate()
2325
const toast = useToast();
2426

2527

28+
const ensureSamePassword = (password: string, rePassword: string) => {
29+
if (password !== rePassword) {
30+
throw Error('Make sure you re-type your new password correctly!')
31+
}
32+
}
33+
34+
const ensureStrongPassword = (password: string) => {
35+
if (password.length < 8) {
36+
throw Error('Password must be 8 characters or more!')
37+
}
38+
}
39+
2640
//TODO: require integration with backend API
27-
const onSubmit = (e: any) => {
41+
const onSubmit = async (e: any) => {
2842
e.preventDefault();
29-
register(username, password).then(response => {
43+
try {
44+
ensureSamePassword(password.trim(), rePassword.trim());
45+
ensureStrongPassword(password.trim());
46+
47+
const response = await register(username.trim(), password.trim());
3048
const user = response.data.user;
3149

3250
dispatch(setUser(user));
3351
navigate('/');
34-
}).catch((err) => {
52+
} catch (err: any) {
3553
console.log(err);
36-
toast({
37-
title: 'Failed to Login',
38-
description: 'Incorrect username or password',
39-
status: 'error',
40-
isClosable: true,
41-
});
42-
})
54+
if (err instanceof AxiosError) {
55+
toast({
56+
title: 'Failed to Register!',
57+
description: 'User is taken!',
58+
status: 'error',
59+
isClosable: true,
60+
});
61+
} else {
62+
toast({
63+
title: 'Failed to Register!',
64+
description: err.message,
65+
status: 'error',
66+
isClosable: true,
67+
});
68+
69+
}
70+
71+
}
4372
}
4473

4574
return (
@@ -58,13 +87,23 @@ export default function RegistrationForm() {
5887

5988
<FormControl id='password' isRequired>
6089
<FormLabel>Password</FormLabel>
61-
<Input type='text'
90+
<Input type='password'
6291
name="password"
6392
value={password}
6493
onChange={(e) => { setPassword(e.target.value) }}
6594
/>
6695
</FormControl>
6796

97+
<FormControl id='repassword' isRequired>
98+
<FormLabel>Re-type Password</FormLabel>
99+
<Input type='password'
100+
name="repassword"
101+
value={rePassword}
102+
onChange={(e) => { setRePassword(e.target.value) }}
103+
/>
104+
</FormControl>
105+
106+
68107
<HStack>
69108

70109
<Button colorScheme="blue" type="submit">

frontend/src/components/profile_page/ChangePasswordCard/ChangePasswordCard.component.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ export default function ChangePasswordCard() {
5151
<FormControl id='newPassword'>
5252
<FormLabel>New Password</FormLabel>
5353
<Input type='text'
54-
name="newPassword"
54+
name="password"
5555
value={newPassword}
5656
onChange={(e) => { setNewPassword(e.target.value) }}
5757
/>
5858
</FormControl>
5959
<FormControl id='retypePassword'>
6060
<FormLabel>Re-type Password</FormLabel>
61-
<Input type='text'
61+
<Input type='password'
6262
name="retypePassword"
6363
value={retypePassword}
6464
onChange={(e) => { setRetypePassword(e.target.value) }}
6565
/>
6666
</FormControl>
6767
<FormControl id='currPassword'>
6868
<FormLabel>Current Password</FormLabel>
69-
<Input type='text'
69+
<Input type='password'
7070
name="currPassword"
7171
value={currPassword}
7272
onChange={(e) => { setCurrPassword(e.target.value) }}

0 commit comments

Comments
 (0)