@@ -26,36 +26,51 @@ export default function SignUpPage() {
2626
2727 // const type = 'user';
2828 if ( password !== confirmPassword ) {
29- router . push ( '/signup' ) ;
3029 setError ( 'Passwords do not match' ) ;
3130 return ;
3231 }
33- const result = await axiosClient . post ( '/users' , {
34- username : name ,
35- email : email ,
36- password : password ,
37- } ) ;
3832
39- if ( result . request . status === 201 ) {
33+ if ( ! agreeTerms ) {
34+ setError ( 'Please agree to the Terms of Service and Privacy Policy' ) ;
35+ return ;
36+ }
37+
38+ try {
39+ const result = await axiosClient . post ( '/users' , {
40+ username : name ,
41+ email : email ,
42+ password : password ,
43+ } ) ;
44+
45+ if ( result . request . status !== 201 ) {
46+ setError ( 'Username or Email already exists' ) ;
47+ return ;
48+ }
49+
4050 // Auto login after account creation
4151 const loginResult = await axiosClient . post ( '/auth/login' , {
4252 email : email ,
4353 password : password ,
4454 } ) ;
55+ if ( loginResult . request . status !== 200 ) {
56+ setError ( 'Unable to login' ) ;
57+ return ;
58+ }
59+
4560 const data = loginResult . data . data ;
46- if ( loginResult . status === 200 ) {
47- const token = data . accessToken ;
48- const res = await login ( token ) ;
49- if ( res ) {
50- setAuth ( true , token , data ) ;
51- router . push ( '/' ) ;
52- return ;
53- }
61+
62+ const token = data . accessToken ;
63+ const res = await login ( token ) ;
64+ if ( res ) {
65+ setAuth ( true , token , data ) ;
66+ router . push ( '/' ) ;
67+ return ;
5468 }
55- setError ( data . error || 'Unable to create account' ) ;
56- } else {
57- setError ( 'Username or Email already exists' ) ;
58- console . error ( 'Sign up failed' ) ;
69+ } catch ( error : unknown ) {
70+ const message =
71+ ( error as { response ?: { data ?: { message ?: string } } } ) ?. response
72+ ?. data ?. message || 'Username or Email already exists' ;
73+ setError ( message ) ;
5974 }
6075 } ;
6176
0 commit comments