Skip to content

Commit

Permalink
Merge pull request #71 from cheehongw/password-strength
Browse files Browse the repository at this point in the history
Password strength
  • Loading branch information
Eclipse-Dominator authored Nov 15, 2023
2 parents c1b6049 + 9c3c628 commit 10b2e64
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/auth/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function LoginForm() {

<FormControl id='password' isRequired>
<FormLabel>Password</FormLabel>
<Input type='text'
<Input type='password'
name="password"
value={password}
onChange={(e) => { setPassword(e.target.value) }}
Expand Down
61 changes: 50 additions & 11 deletions frontend/src/components/auth/ResgistrationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,61 @@ import { setUser } from '../../reducers/authSlice';
import { useDispatch } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { register } from '../../api/auth';
import { AxiosError } from 'axios';

export default function RegistrationForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [rePassword, setRePassword] = useState('');
const dispatch = useDispatch()
const navigate = useNavigate()
const toast = useToast();


const ensureSamePassword = (password: string, rePassword: string) => {
if (password !== rePassword) {
throw Error('Make sure you re-type your new password correctly!')
}
}

const ensureStrongPassword = (password: string) => {
if (password.length < 8) {
throw Error('Password must be 8 characters or more!')
}
}

//TODO: require integration with backend API
const onSubmit = (e: any) => {
const onSubmit = async (e: any) => {
e.preventDefault();
register(username, password).then(response => {
try {
ensureSamePassword(password.trim(), rePassword.trim());
ensureStrongPassword(password.trim());

const response = await register(username.trim(), password.trim());
const user = response.data.user;

dispatch(setUser(user));
navigate('/');
}).catch((err) => {
} catch (err: any) {
console.log(err);
toast({
title: 'Failed to Login',
description: 'Incorrect username or password',
status: 'error',
isClosable: true,
});
})
if (err instanceof AxiosError) {
toast({
title: 'Failed to Register!',
description: 'User is taken!',
status: 'error',
isClosable: true,
});
} else {
toast({
title: 'Failed to Register!',
description: err.message,
status: 'error',
isClosable: true,
});

}

}
}

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

<FormControl id='password' isRequired>
<FormLabel>Password</FormLabel>
<Input type='text'
<Input type='password'
name="password"
value={password}
onChange={(e) => { setPassword(e.target.value) }}
/>
</FormControl>

<FormControl id='repassword' isRequired>
<FormLabel>Re-type Password</FormLabel>
<Input type='password'
name="repassword"
value={rePassword}
onChange={(e) => { setRePassword(e.target.value) }}
/>
</FormControl>


<HStack>

<Button colorScheme="blue" type="submit">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ export default function ChangePasswordCard() {
<FormControl id='newPassword'>
<FormLabel>New Password</FormLabel>
<Input type='text'
name="newPassword"
name="password"
value={newPassword}
onChange={(e) => { setNewPassword(e.target.value) }}
/>
</FormControl>
<FormControl id='retypePassword'>
<FormLabel>Re-type Password</FormLabel>
<Input type='text'
<Input type='password'
name="retypePassword"
value={retypePassword}
onChange={(e) => { setRetypePassword(e.target.value) }}
/>
</FormControl>
<FormControl id='currPassword'>
<FormLabel>Current Password</FormLabel>
<Input type='text'
<Input type='password'
name="currPassword"
value={currPassword}
onChange={(e) => { setCurrPassword(e.target.value) }}
Expand Down

0 comments on commit 10b2e64

Please sign in to comment.