@@ -14,6 +14,7 @@ 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 ( '' ) ;
@@ -23,23 +24,42 @@ export default function RegistrationForm() {
23
24
const toast = useToast ( ) ;
24
25
25
26
27
+ const ensureStrongPassword = ( password : string ) => {
28
+ if ( password . length < 8 ) {
29
+ throw Error ( 'Password must be 8 characters or more!' )
30
+ }
31
+ }
32
+
26
33
//TODO: require integration with backend API
27
- const onSubmit = ( e : any ) => {
34
+ const onSubmit = async ( e : any ) => {
28
35
e . preventDefault ( ) ;
29
- register ( username , password ) . then ( response => {
36
+ try {
37
+ ensureStrongPassword ( password . trim ( ) ) ;
38
+ const response = await register ( username . trim ( ) , password . trim ( ) ) ;
30
39
const user = response . data . user ;
31
40
32
41
dispatch ( setUser ( user ) ) ;
33
42
navigate ( '/' ) ;
34
- } ) . catch ( ( err ) => {
43
+ } catch ( err : any ) {
35
44
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
+ }
43
63
}
44
64
45
65
return (
0 commit comments