@@ -39,55 +39,83 @@ const Auth = () => {
3939 e . preventDefault ( ) ;
4040 setLoading ( true ) ;
4141
42- if ( mode === 'signup' ) {
43- if ( ! formData . username . trim ( ) ) {
44- toast ( {
45- title : "Username Required" ,
46- description : "Please enter a username" ,
47- variant : "destructive"
48- } ) ;
49- setLoading ( false ) ;
50- return ;
51- }
42+ try {
43+ if ( mode === 'signup' ) {
44+ if ( ! formData . username . trim ( ) ) {
45+ toast ( {
46+ title : "Username Required" ,
47+ description : "Please enter a username" ,
48+ variant : "destructive"
49+ } ) ;
50+ setLoading ( false ) ;
51+ return ;
52+ }
5253
53- const { error } = await signUpWithEmail (
54- formData . email ,
55- formData . password ,
56- formData . username
57- ) ;
58-
59- if ( error ) {
60- toast ( {
61- title : "Sign Up Failed" ,
62- description : error . message ,
63- variant : "destructive"
64- } ) ;
65- } else {
66- toast ( {
67- title : "Check Your Email! 📧" ,
68- description : "We sent you a confirmation link" ,
69- } ) ;
70- setMode ( 'signin' ) ;
71- }
72- } else {
73- const { error } = await signInWithEmail ( formData . email , formData . password ) ;
74-
75- if ( error ) {
76- toast ( {
77- title : "Sign In Failed" ,
78- description : error . message ,
79- variant : "destructive"
80- } ) ;
54+ const { data, error } = await signUpWithEmail (
55+ formData . email ,
56+ formData . password ,
57+ formData . username
58+ ) ;
59+
60+ if ( error ) {
61+ toast ( {
62+ title : "Sign Up Failed" ,
63+ description : error . message ,
64+ variant : "destructive"
65+ } ) ;
66+ } else if ( data . user ) {
67+ // Check if email confirmation is required
68+ if ( data . user . identities && data . user . identities . length === 0 ) {
69+ toast ( {
70+ title : "Email Already Registered" ,
71+ description : "Please sign in with your existing account" ,
72+ variant : "destructive"
73+ } ) ;
74+ } else if ( data . session ) {
75+ // Auto-confirmed, user is logged in
76+ toast ( {
77+ title : "Account Created! 🎉" ,
78+ description : "Redirecting to quiz..." ,
79+ } ) ;
80+ setTimeout ( ( ) => navigate ( '/quiz' ) , 500 ) ;
81+ } else {
82+ // Email confirmation required
83+ toast ( {
84+ title : "Check Your Email! 📧" ,
85+ description : "We sent you a confirmation link. If you don't see it, check spam folder." ,
86+ } ) ;
87+ setMode ( 'signin' ) ;
88+ }
89+ }
8190 } else {
82- toast ( {
83- title : "Welcome Back! 🎉" ,
84- description : "Redirecting to quiz..." ,
85- } ) ;
86- setTimeout ( ( ) => navigate ( '/quiz' ) , 500 ) ;
91+ const { data, error } = await signInWithEmail ( formData . email , formData . password ) ;
92+
93+ if ( error ) {
94+ toast ( {
95+ title : "Sign In Failed" ,
96+ description : error . message === "Invalid login credentials"
97+ ? "Wrong email or password. Please try again."
98+ : error . message ,
99+ variant : "destructive"
100+ } ) ;
101+ } else if ( data . user ) {
102+ toast ( {
103+ title : "Welcome Back! 🎉" ,
104+ description : "Redirecting to quiz..." ,
105+ } ) ;
106+ setTimeout ( ( ) => navigate ( '/quiz' ) , 500 ) ;
107+ }
87108 }
109+ } catch ( err ) {
110+ toast ( {
111+ title : "Error" ,
112+ description : "Something went wrong. Please try again." ,
113+ variant : "destructive"
114+ } ) ;
115+ console . error ( 'Auth error:' , err ) ;
116+ } finally {
117+ setLoading ( false ) ;
88118 }
89-
90- setLoading ( false ) ;
91119 } ;
92120
93121 return (
0 commit comments