1
1
"use client" ;
2
2
3
3
import React , { useState , useEffect } from "react" ;
4
- import axios from "axios" ;
5
4
import { useNavigate } from "react-router-dom" ;
6
5
import { toast } from "sonner" ;
7
6
@@ -11,7 +10,7 @@ import { Button } from "@/components/ui/button";
11
10
import { Input } from "@/components/ui/input" ;
12
11
import { Label } from "@/components/ui/label" ;
13
12
14
- interface UserAuthFormProps extends React . HTMLAttributes < HTMLDivElement > { }
13
+ interface UserAuthFormProps extends React . HTMLAttributes < HTMLDivElement > { }
15
14
interface PasswordRules {
16
15
minLength : boolean ;
17
16
containsUpper : boolean ;
@@ -26,18 +25,15 @@ const backendUrl = import.meta.env.VITE_BACKEND_URL || "http://localhost:5000";
26
25
export function UserAuthForm ( { className, ...props } : UserAuthFormProps ) {
27
26
const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
28
27
const [ username , setUsername ] = useState < string > ( "" ) ;
29
- const [ isUsernameAvailable , setIsUsernameAvailable ] = useState <
30
- boolean | null
31
- > ( null ) ;
28
+ const [ isUsernameAvailable , setIsUsernameAvailable ] = useState < boolean | null > ( null ) ;
32
29
const [ email , setEmail ] = useState < string > ( "" ) ;
33
30
const [ password , setPassword ] = useState < string > ( "" ) ;
34
31
const [ usernameError , setUsernameError ] = useState < string > ( "" ) ;
35
- const [ passwordRules , setPasswordRules ] = useState < PasswordRules | null > (
36
- null
37
- ) ;
32
+ const [ passwordRules , setPasswordRules ] = useState < PasswordRules | null > ( null ) ;
38
33
const [ isPasswordValid , setIsPasswordValid ] = useState < boolean > ( false ) ;
39
- const navigate = useNavigate ( ) ; // Hook for navigation
34
+ const navigate = useNavigate ( ) ;
40
35
36
+ // Username availability check with debounce
41
37
useEffect ( ( ) => {
42
38
const checkUsernameAvailability = async ( ) => {
43
39
if ( username ) {
@@ -56,10 +52,10 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
56
52
return ;
57
53
}
58
54
setUsernameError ( "" ) ;
59
- const response = await axios . get ( ` ${ backendUrl } /check_username` , {
60
- params : { username } ,
61
- } ) ;
62
- setIsUsernameAvailable ( response . data . available ) ;
55
+
56
+ const response = await fetch ( ` ${ backendUrl } /check_username? username= ${ username } ` ) ;
57
+ const data = await response . json ( ) ;
58
+ setIsUsernameAvailable ( data . available ) ;
63
59
} catch ( error ) {
64
60
console . error ( "Error checking username availability:" , error ) ;
65
61
}
@@ -76,6 +72,7 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
76
72
return ( ) => clearTimeout ( delayDebounceFn ) ;
77
73
} , [ username ] ) ;
78
74
75
+ // Password validation rules
79
76
const validatePassword = ( password : string ) : PasswordRules => {
80
77
return {
81
78
minLength : password . length >= 6 ,
@@ -107,6 +104,7 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
107
104
noWhitespace : "Should not contain spaces." ,
108
105
} ;
109
106
107
+ // Form submission handler
110
108
async function onSubmit ( event : React . SyntheticEvent ) {
111
109
event . preventDefault ( ) ;
112
110
setIsLoading ( true ) ;
@@ -116,34 +114,36 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
116
114
}
117
115
118
116
try {
119
- await axios . post ( `${ backendUrl } /signup` , { username, email, password } ) ;
117
+ const response = await fetch ( `${ backendUrl } /signup` , {
118
+ method : "POST" ,
119
+ headers : {
120
+ "Content-Type" : "application/json" ,
121
+ } ,
122
+ body : JSON . stringify ( { username, email, password } ) ,
123
+ } ) ;
124
+
125
+ if ( ! response . ok ) {
126
+ const errorData = await response . json ( ) ;
127
+ throw new Error ( errorData . message || "Signup failed" ) ;
128
+ }
129
+
120
130
toast . success ( "Signup successful" , {
121
131
description : "You can now log in with your new account." ,
122
132
action : {
123
133
label : "Login" ,
124
134
onClick : ( ) => navigate ( "/login" ) ,
125
135
} ,
126
136
} ) ;
127
- navigate ( "/login" ) ; // Redirect to login page on successful signup
128
- } catch ( err : any ) {
129
- if ( err . response && err . response . data && err . response . data . message ) {
130
- toast . error ( err . response . data . message , {
131
- description : "Please check your details and try again." ,
132
- action : {
133
- label : "Try again" ,
134
- onClick : ( ) => console . log ( "Try again clicked" ) ,
135
- } ,
136
- } ) ;
137
- } else {
138
- toast . error ( "Signup failed" , {
139
- description : "There was a problem with your request." ,
140
- action : {
141
- label : "Try again" ,
142
- onClick : ( ) => console . log ( "Try again clicked" ) ,
143
- } ,
144
- } ) ;
145
- }
146
- console . error ( "Error during signup:" , err ) ;
137
+ navigate ( "/login" ) ;
138
+ } catch ( error ) {
139
+ toast . error ( `Signup failed ${ error } ` , {
140
+ description : "Please check your details and try again." ,
141
+ action : {
142
+ label : "Try again" ,
143
+ onClick : ( ) => console . log ( "Try again clicked" ) ,
144
+ } ,
145
+ } ) ;
146
+ console . error ( "Error during signup:" , error ) ;
147
147
} finally {
148
148
setIsLoading ( false ) ;
149
149
}
0 commit comments