Skip to content

Commit 58f776a

Browse files
committed
Password strength
1 parent b2ffaef commit 58f776a

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

frontend/src/components/auth/ResgistrationForm.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ 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('');
@@ -23,23 +24,42 @@ export default function RegistrationForm() {
2324
const toast = useToast();
2425

2526

27+
const ensureStrongPassword = (password: string) => {
28+
if (password.length < 8) {
29+
throw Error('Password must be 8 characters or more!')
30+
}
31+
}
32+
2633
//TODO: require integration with backend API
27-
const onSubmit = (e: any) => {
34+
const onSubmit = async (e: any) => {
2835
e.preventDefault();
29-
register(username, password).then(response => {
36+
try {
37+
ensureStrongPassword(password.trim());
38+
const response = await register(username.trim(), password.trim());
3039
const user = response.data.user;
3140

3241
dispatch(setUser(user));
3342
navigate('/');
34-
}).catch((err) => {
43+
} catch (err: any) {
3544
console.log(err);
36-
toast({
37-
title: 'Failed to Login',
38-
description: 'Incorrect username or password',
39-
status: 'error',
40-
isClosable: true,
41-
});
42-
})
45+
if (err instanceof AxiosError) {
46+
toast({
47+
title: 'Failed to Register!',
48+
description: 'User is taken!',
49+
status: 'error',
50+
isClosable: true,
51+
});
52+
} else {
53+
toast({
54+
title: 'Failed to Register!',
55+
description: err.message,
56+
status: 'error',
57+
isClosable: true,
58+
});
59+
60+
}
61+
62+
}
4363
}
4464

4565
return (

0 commit comments

Comments
 (0)