@@ -14,32 +14,61 @@ import { setUser } from '../../reducers/authSlice';
14
14
import { useDispatch } from 'react-redux' ;
15
15
import { useNavigate } from 'react-router-dom' ;
16
16
import { register } from '../../api/auth' ;
17
+ import { AxiosError } from 'axios' ;
17
18
18
19
export default function RegistrationForm ( ) {
19
20
const [ username , setUsername ] = useState ( '' ) ;
20
21
const [ password , setPassword ] = useState ( '' ) ;
22
+ const [ rePassword , setRePassword ] = useState ( '' ) ;
21
23
const dispatch = useDispatch ( )
22
24
const navigate = useNavigate ( )
23
25
const toast = useToast ( ) ;
24
26
25
27
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
+
26
40
//TODO: require integration with backend API
27
- const onSubmit = ( e : any ) => {
41
+ const onSubmit = async ( e : any ) => {
28
42
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 ( ) ) ;
30
48
const user = response . data . user ;
31
49
32
50
dispatch ( setUser ( user ) ) ;
33
51
navigate ( '/' ) ;
34
- } ) . catch ( ( err ) => {
52
+ } catch ( err : any ) {
35
53
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
+ }
43
72
}
44
73
45
74
return (
@@ -58,13 +87,23 @@ export default function RegistrationForm() {
58
87
59
88
< FormControl id = 'password' isRequired >
60
89
< FormLabel > Password</ FormLabel >
61
- < Input type = 'text '
90
+ < Input type = 'password '
62
91
name = "password"
63
92
value = { password }
64
93
onChange = { ( e ) => { setPassword ( e . target . value ) } }
65
94
/>
66
95
</ FormControl >
67
96
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
+
68
107
< HStack >
69
108
70
109
< Button colorScheme = "blue" type = "submit" >
0 commit comments