@@ -11,6 +11,9 @@ import ErrorBoundary from '@/components/error/ErrorBoundary';
1111import { useToast } from '@/components/ui/ToastProvider' ;
1212import { useStellarWalletAuth } from '@/hooks/useStellarWalletAuth' ;
1313import { useAuth } from '@/hooks/useAuth' ;
14+ import { useGoogleAuth } from '@/hooks/useGoogleAuth' ;
15+ import { useGuestSession } from '@/hooks/useGuestSession' ;
16+ import { signIn } from '@/lib/api/authApi' ;
1417import { WalletType } from '@/lib/stellar/types' ;
1518import WalletModal , { WalletType as ModalWalletType } from '@/components/ui/WalletModal' ;
1619import { useWalletModal } from '@/hooks/useWalletModal' ;
@@ -26,6 +29,8 @@ const SignInPage = () => {
2629 clearError,
2730 } = useStellarWalletAuth ( ) ;
2831 const { loginSuccess, loginFailure, setLoading } = useAuth ( ) ;
32+ const { signInWithGoogle } = useGoogleAuth ( ) ;
33+ const { startGuestSession, isStarting : isGuestStarting } = useGuestSession ( ) ;
2934 const { isOpen : isWalletModalOpen , openModal : openWalletModal , closeModal : closeWalletModal } = useWalletModal ( ) ;
3035 const [ formData , setFormData ] = useState ( {
3136 username : '' ,
@@ -78,123 +83,59 @@ const SignInPage = () => {
7883 }
7984
8085 try {
81- const response = await fetch ( 'http://localhost:3000/auth/signIn' , {
82- method : 'POST' ,
83- headers : {
84- 'Content-Type' : 'application/json' ,
85- } ,
86- body : JSON . stringify ( {
87- email : formData . username ,
88- password : formData . password
89- } ) ,
90- } ) ;
91-
92- // Checking if response is ok before trying to parse JSON
93- if ( ! response . ok ) {
94- try {
95- const errorData = await response . json ( ) ;
96- // Handle specific error messages from server
97- if ( response . status === 401 ) {
98- const errorMsg = 'Invalid email or password.' ;
99- showError ( 'Login Failed' , errorMsg ) ;
100- loginFailure ( errorMsg ) ;
101- setIsLoading ( false ) ;
102- setLoading ( false ) ;
103- } else if ( response . status === 404 ) {
104- const errorMsg = 'Account not found.' ;
105- showError ( 'Account Not Found' , errorMsg ) ;
106- loginFailure ( errorMsg ) ;
107- setIsLoading ( false ) ;
108- setLoading ( false ) ;
109- } else if ( response . status === 400 ) {
110- const errorMsg = errorData . message || 'Invalid input.' ;
111- showError ( 'Invalid Input' , errorMsg ) ;
112- loginFailure ( errorMsg ) ;
113- setIsLoading ( false ) ;
114- setLoading ( false ) ;
115- } else if ( response . status >= 500 ) {
116- const errorMsg = 'Server error. Please try again later.' ;
117- showError ( 'Server Error' , errorMsg ) ;
118- loginFailure ( errorMsg ) ;
119- setIsLoading ( false ) ;
120- setLoading ( false ) ;
121- } else {
122- const errorMsg = errorData . message || 'Login failed. Please try again.' ;
123- showError ( 'Login Failed' , errorMsg ) ;
124- loginFailure ( errorMsg ) ;
125- setIsLoading ( false ) ;
126- setLoading ( false ) ;
127- }
128- } catch {
129- // If response isn't JSON, use status text or default message
130- if ( response . status === 401 ) {
131- const errorMsg = 'Invalid email or password.' ;
132- showError ( 'Login Failed' , errorMsg ) ;
133- loginFailure ( errorMsg ) ;
134- setIsLoading ( false ) ;
135- setLoading ( false ) ;
136- } else if ( response . status === 404 ) {
137- const errorMsg = 'Account not found.' ;
138- showError ( 'Account Not Found' , errorMsg ) ;
139- loginFailure ( errorMsg ) ;
140- setIsLoading ( false ) ;
141- setLoading ( false ) ;
142- } else {
143- const errorMsg = `Login failed: ${ response . statusText || 'Please try again.' } ` ;
144- showError ( 'Login Failed' , errorMsg ) ;
145- loginFailure ( errorMsg ) ;
146- setIsLoading ( false ) ;
147- setLoading ( false ) ;
148- }
149- }
150- setIsLoading ( false ) ;
151- setLoading ( false ) ;
152- return ;
153- }
154-
155- // Parse JSON only if response is ok
156- const data = await response . json ( ) ;
86+ const data = await signIn ( formData . username , formData . password ) ;
15787
15888 if ( data . accessToken && data . refreshToken ) {
159- // Update Redux state with both tokens
160- const user = {
161- id : data . user ?. id || formData . username ,
89+ const user = data . user ?? {
90+ id : formData . username ,
16291 email : formData . username ,
163- username : data . user ?. username || formData . username . split ( '@' ) [ 0 ] ,
92+ username : formData . username . split ( '@' ) [ 0 ] ,
16493 } ;
165-
94+
16695 loginSuccess ( user , data . accessToken , data . refreshToken ) ;
167-
168- // Show success toast
16996 showSuccess ( 'Login Successful' , 'Welcome back!' ) ;
170- setIsLoading ( false ) ;
171- setLoading ( false ) ;
172-
173- // Redirect to dashboard
17497 router . push ( '/dashboard' ) ;
175- } else {
176- const errorMsg = 'Invalid response from server. Please try again.' ;
177- showError ( 'Invalid Response' , errorMsg ) ;
178- loginFailure ( errorMsg ) ;
179- setIsLoading ( false ) ;
180- setLoading ( false ) ;
98+ return ;
18199 }
182- } catch ( error ) {
183- console . error ( 'Sign in error:' , error ) ;
184- const errorMsg = 'Network error.' ;
185- showError ( 'Network Error' , errorMsg ) ;
100+
101+ const errorMsg = 'Invalid response from server. Please try again.' ;
102+ showError ( 'Invalid Response' , errorMsg ) ;
186103 loginFailure ( errorMsg ) ;
187- setIsLoading ( false ) ;
188- setLoading ( false ) ;
104+ } catch ( error ) {
105+ const message =
106+ error instanceof Error ? error . message : 'Login failed. Please try again.' ;
107+ const isNetwork = message === 'Unexpected error occurred' || / n e t w o r k / i. test ( message ) ;
108+ showError ( isNetwork ? 'Network Error' : 'Login Failed' , message ) ;
109+ loginFailure ( message ) ;
189110 } finally {
190111 setIsLoading ( false ) ;
191112 setLoading ( false ) ;
192113 }
193114 } ;
194115
195- const handleGoogleSignIn = ( ) => {
196- showInfo ( 'Google Sign-In' , 'Redirecting to Google authentication...' ) ;
197- window . location . href = "http://localhost:3000/auth/google-authentication" ;
116+ const handleGoogleSignIn = async ( ) => {
117+ try {
118+ showInfo ( 'Google Sign-In' , 'Opening Google authentication...' ) ;
119+ await signInWithGoogle ( ) ;
120+ showSuccess ( 'Login Successful' , 'Welcome back!' ) ;
121+ router . push ( '/dashboard' ) ;
122+ } catch ( error ) {
123+ const message =
124+ error instanceof Error ? error . message : 'Google sign-in failed' ;
125+ showError ( 'Google Sign-In Failed' , message ) ;
126+ }
127+ } ;
128+
129+ const handleGuestPlay = async ( ) => {
130+ try {
131+ await startGuestSession ( ) ;
132+ showSuccess ( 'Guest session started' , 'You have 15 minutes to browse puzzles.' ) ;
133+ router . push ( '/puzzles' ) ;
134+ } catch ( error ) {
135+ const message =
136+ error instanceof Error ? error . message : 'Could not start a guest session' ;
137+ showError ( 'Guest Play Failed' , message ) ;
138+ }
198139 } ;
199140
200141 const handleWalletSelect = async ( walletType : ModalWalletType ) => {
@@ -339,6 +280,15 @@ const SignInPage = () => {
339280 { isLoggingIn && 'Verifying...' }
340281 { ! isConnecting && ! isSigning && ! isLoggingIn && 'Connect Wallet' }
341282 </ button >
283+
284+ < button
285+ type = "button"
286+ onClick = { handleGuestPlay }
287+ disabled = { isGuestStarting }
288+ className = "w-full h-12 border-2 border-slate-600 text-slate-200 rounded-lg hover:bg-slate-800/80 transition-colors disabled:opacity-50"
289+ >
290+ { isGuestStarting ? 'Starting guest session...' : 'Continue as guest' }
291+ </ button >
342292 </ div >
343293
344294 { /* Terms and Privacy */ }
0 commit comments