@@ -156,61 +156,8 @@ <h2>Sign In</h2>
156156 <!-- Toast Container -->
157157 < div class ="toast-container " id ="toastContainer "> </ div >
158158
159- < script >
160- async function handleLogin ( e ) {
161- e . preventDefault ( ) ;
162-
163- const email = document . getElementById ( 'loginEmail' ) . value . trim ( ) ;
164- const password = document . getElementById ( 'loginPassword' ) . value . trim ( ) ;
165-
166- const btn = document . getElementById ( 'loginBtn' ) ;
167- btn . disabled = true ;
168- btn . innerHTML = "Signing in..." ;
169-
170- try {
171- const res = await fetch (
172- "https://evalis-ai.simpaticohrconsultancy.workers.dev/api/auth/login" ,
173- {
174- method : "POST" ,
175- headers : { "Content-Type" : "application/json" } ,
176- body : JSON . stringify ( { email, password } )
177- }
178- ) ;
179-
180- const data = await res . json ( ) ;
181-
182- if ( ! res . ok || data . error ) {
183- alert ( data . error || "Login failed" ) ;
184- btn . disabled = false ;
185- btn . innerHTML = "Sign In" ;
186- return ;
187- }
188-
189- // Store worker JWT
190- localStorage . setItem ( "simpatico_token" , data . token ) ;
191- localStorage . setItem ( "simpatico_user" , JSON . stringify ( data . user ) ) ;
192-
193- // Redirect based on role (MATCHES YOUR REAL STRUCTURE)
194- if ( data . user . role === "super_admin" ) {
195- window . location . href = "/platform/super-admin.html" ;
196- }
197- } else if ( data . user . role === "company_admin" ) {
198- window . location . href = "/dashboard/hr.html" ;
199- } else if ( data . user . role === "candidate" ) {
200- window . location . href = "/dashboard/candidate.html" ;
201- } else {
202- alert ( "Unknown role" ) ;
203- }
204- } catch ( err ) {
205- alert ( "Network error" ) ;
206- }
207-
208- btn . disabled = false ;
209- btn . innerHTML = "Sign In" ;
210- }
211- </ script >
212159 < script >
213- // Role Tabs
160+ // Role Tabs — defined first so selectedRole is available to handleLogin
214161 let selectedRole = 'hr' ;
215162 document . querySelectorAll ( '.role-tab' ) . forEach ( tab => {
216163 tab . addEventListener ( 'click' , ( ) => {
@@ -219,6 +166,60 @@ <h2>Sign In</h2>
219166 selectedRole = tab . dataset . role ;
220167 } ) ;
221168 } ) ;
169+
170+ async function handleLogin ( e ) {
171+ e . preventDefault ( ) ;
172+
173+ const email = document . getElementById ( 'loginEmail' ) . value . trim ( ) ;
174+ const password = document . getElementById ( 'loginPassword' ) . value . trim ( ) ;
175+
176+ const btn = document . getElementById ( 'loginBtn' ) ;
177+ btn . disabled = true ;
178+ btn . innerHTML = '<i class="fas fa-spinner fa-spin"></i> Signing in...' ;
179+
180+ try {
181+ const res = await fetch (
182+ "https://evalis-ai.simpaticohrconsultancy.workers.dev/api/auth/login" ,
183+ {
184+ method : "POST" ,
185+ headers : { "Content-Type" : "application/json" } ,
186+ // FIX 1: send role so the worker doesn't crash on undefined
187+ body : JSON . stringify ( { email, password, role : selectedRole } )
188+ }
189+ ) ;
190+
191+ const data = await res . json ( ) ;
192+
193+ if ( ! res . ok || data . error ) {
194+ alert ( data . error || "Login failed" ) ;
195+ btn . disabled = false ;
196+ btn . innerHTML = '<i class="fas fa-sign-in-alt"></i> Sign In' ;
197+ return ;
198+ }
199+
200+ // Store worker JWT
201+ localStorage . setItem ( "simpatico_token" , data . token ) ;
202+ localStorage . setItem ( "simpatico_user" , JSON . stringify ( data . user ) ) ;
203+
204+ // FIX 2: corrected if/else chain (removed stray closing brace that broke the block)
205+ if ( data . user . role === "super_admin" ) {
206+ window . location . href = "/platform/super-admin.html" ;
207+ } else if ( data . user . role === "company_admin" ) {
208+ window . location . href = "/dashboard/hr.html" ;
209+ } else if ( data . user . role === "candidate" ) {
210+ window . location . href = "/dashboard/candidate.html" ;
211+ } else {
212+ alert ( "Unknown role: " + data . user . role ) ;
213+ btn . disabled = false ;
214+ btn . innerHTML = '<i class="fas fa-sign-in-alt"></i> Sign In' ;
215+ }
216+
217+ } catch ( err ) {
218+ alert ( "Network error: " + err . message ) ;
219+ btn . disabled = false ;
220+ btn . innerHTML = '<i class="fas fa-sign-in-alt"></i> Sign In' ;
221+ }
222+ }
222223 </ script >
223224</ body >
224225</ html >
0 commit comments