@@ -31,39 +31,75 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
3131 const onSubmit = async ( data : { email : string ; password : string } ) => {
3232 console . log ( "🔐 Attempting login with:" , data ) ;
3333 setLogging ( true ) ;
34- // final working
3534 try {
36- // Step 1: Clear previous session
35+ // Step 1: Clear previous session if it exists
36+ const sid = localStorage . getItem ( "sid" ) ;
3737 localStorage . removeItem ( "sid" ) ;
3838 console . log ( "🧹 Cleared localStorage token" ) ;
3939
40- await fetch ( "https://test.neotechis.com/api/method/logout" , {
41- method : "GET" ,
42- credentials : "include" ,
43- } ) ;
44- console . log ( "🔄 Forced logout of any existing session" ) ;
40+ if ( sid ) {
41+ // Only call logout if there was a previous session
42+ try {
43+ const logoutRes = await fetch (
44+ "https://test.neotechis.com/api/method/alphax_erp.api.login.logout_user" ,
45+ {
46+ method : "GET" ,
47+ credentials : "include" ,
48+ }
49+ ) ;
50+ console . log ( "🔄 Logout response status:" , logoutRes . status ) ;
51+
52+ if ( ! logoutRes . ok ) {
53+ console . warn ( "⚠️ Logout failed:" , logoutRes . status , await logoutRes . text ( ) ) ;
54+ // Proceed with login even if logout fails (e.g., 403 for no active session)
55+ } else {
56+ console . log ( "🔄 Forced logout of any existing session" ) ;
57+ }
58+ } catch ( logoutError ) {
59+ console . warn ( "⚠️ Error during logout, proceeding with login:" , logoutError ) ;
60+ // Continue with login even if logout fails
61+ }
62+ } else {
63+ console . log ( "ℹ️ No previous session, skipping logout" ) ;
64+ }
4565
4666 // Step 2: Login
47- const loginRes = await fetch ( "https://test.neotechis.com/api/method/alphax_erp.api.login.login" , {
48- method : "POST" ,
49- headers : {
50- "Content-Type" : "application/json" ,
51- } ,
52- credentials : "include" ,
53- body : JSON . stringify ( {
54- email : data . email ,
55- password : data . password ,
56- } ) ,
57- } ) ;
67+ const loginRes = await fetch (
68+ "https://test.neotechis.com/api/method/alphax_erp.api.login.login" ,
69+ {
70+ method : "POST" ,
71+ headers : {
72+ "Content-Type" : "application/json" ,
73+ } ,
74+ credentials : "include" ,
75+ body : JSON . stringify ( {
76+ email : data . email ,
77+ password : data . password ,
78+ } ) ,
79+ }
80+ ) ;
5881
5982 console . log ( "📥 Login response status:" , loginRes . status ) ;
6083
6184 if ( ! loginRes . ok ) {
6285 const err = await loginRes . json ( ) ;
6386 console . error ( "❌ Login failed:" , err ) ;
6487
65- const messages = JSON . parse ( err . _server_messages || "[]" ) ;
66- const errorMsg = messages . length ? messages [ 0 ] : err . message || "Login failed" ;
88+ let errorMsg = "Login failed" ;
89+ if ( loginRes . status === 401 ) {
90+ errorMsg = err . message || "Invalid email or password" ;
91+ } else if ( loginRes . status === 500 ) {
92+ errorMsg = err . message || "Something went wrong, please try again later." ;
93+ } else {
94+ try {
95+ const messages = JSON . parse ( err . _server_messages || "[]" ) ;
96+ errorMsg = messages . length ? messages [ 0 ] : err . message || "Login failed" ;
97+ } catch {
98+ errorMsg = err . message || "Unexpected error occurred" ;
99+ }
100+ }
101+
102+ enqueueSnackbar ( errorMsg , { variant : "error" } ) ;
67103 throw new Error ( errorMsg ) ;
68104 }
69105
@@ -89,12 +125,12 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
89125
90126 setTimeout ( ( ) => {
91127 const redirectUrl = url || "https://test.neotechis.com/dashboard" ;
92- console . log ( "➡️ Redirecting to :" , redirectUrl ) ;
93- location . replace ( redirectUrl ) ;
128+ console . log ( "➡️ Opening in new tab :" , redirectUrl ) ;
129+ window . open ( redirectUrl , "_blank" ) ;
94130 } , 500 ) ;
95131 } catch ( error ) {
96132 console . error ( "🚨 Login error:" , error ) ;
97- enqueueSnackbar ( ( error as Error ) . message || "Login failed ", {
133+ enqueueSnackbar ( "An unexpected error occurred. Please try again. ", {
98134 variant : "error" ,
99135 } ) ;
100136 } finally {
@@ -105,28 +141,28 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
105141 return (
106142 < div
107143 className = { cn (
108- "w-full mx-4 my-4 lg:mx-0 lg:mt-7 bg-white border border-gray-200 rounded-xl shadow-sm dark:bg-gray-800 dark:border-gray-700 md:max-w-[450px]" ,
144+ "w-full mx-4 my-4 lg:mx-0 lg:mt-7 bg-white border border-gray-200 rounded-xl shadow-sm dark:bg-gray-800 dark:border-gray-700 md:max-w-[450px] " ,
109145 loginSuccess && "md:max-w-max"
110146 ) }
111147 >
112148 { ! loginSuccess && (
113149 < div className = "p-4 sm:p-7" >
114- < h1 className = "block text-2xl font-bold text-gray-800 dark:text-white text-center " >
150+ < h1 className = "text-4xl font-bold text-center text- gray-800 dark:text-[#00f0ff] neon-glow " >
115151 Sign in
116152 </ h1 >
117153
118154 < div className = "mt-5" >
119155 < form className = "w-full" onSubmit = { handleSubmit ( onSubmit ) } >
120156 < div className = "grid gap-y-4" >
121157 < div >
122- < label htmlFor = "email" className = "block text-sm mb-2 dark:text-white" >
158+ < label htmlFor = "email" className = "block text-base mb-2 dark:text-white" >
123159 Email < span className = "text-red-400" > *</ span >
124160 </ label >
125161 < input
126162 type = "email"
127163 id = "email"
128- className = "py-3 px-4 block w-full border border-gray-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-slate-900 dark:border-gray-700 dark:text-gray-400 dark: focus:ring-gray-600 "
129- placeholder = "Enter your lovely email "
164+ className = "py-3 px-4 block w-full border rounded-lg text-sm dark:bg-slate-900 dark:border-gray-700 dark:text-gray-400 placeholder:text-gray-400 focus:outline-none focus:border-[#774A67] focus:shadow-[0_0_15px_#774A67] transition duration-300 "
165+ placeholder = "Enter your E-mail "
130166 { ...register ( "email" , {
131167 required : "Email is required" ,
132168 pattern : {
@@ -144,13 +180,13 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
144180 </ div >
145181
146182 < div >
147- < label htmlFor = "password" className = "block text-sm mb-2 dark:text-white" >
183+ < label htmlFor = "password" className = "block text-base mb-2 dark:text-white" >
148184 Password < span className = "text-red-400" > *</ span >
149185 </ label >
150186 < input
151187 type = "password"
152188 id = "password"
153- className = "py-3 px-4 block w-full border border-gray-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-slate-900 dark:border-gray-700 dark:text-gray-400 dark: focus:ring-gray-600 "
189+ className = "py-3 px-4 block w-full border rounded-lg text-sm dark:bg-slate-900 dark:border-gray-700 dark:text-gray-400 placeholder:text-gray-400 focus:outline-none focus:border-[#774A67] focus:shadow-[0_0_15px_#774A67] transition duration-300 "
154190 placeholder = "Enter your password"
155191 { ...register ( "password" , {
156192 required : "Password is required" ,
@@ -171,7 +207,7 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
171207 < button
172208 type = "submit"
173209 disabled = { logging }
174- className = "w-full mt-4 py-3 px-4 inline-flex justify-center items-center gap-x-2 text-sm font-semibold rounded-lg border border-transparent bg-[#774A67] text-white hover:bg-[#774A67] disabled:opacity-50 disabled:pointer-events- none dark: focus:outline-none dark: focus:ring-1 dark: focus:ring-gray-600 "
210+ className = "w-full mt-4 py-3 px-4 inline-flex justify-center items-center gap-x-2 text-sm font-semibold rounded-lg border border-transparent bg-[#774A67] text-white hover:bg-[#774A67] hover:shadow-[0_0_15px_#774A67] focus:outline- none focus:ring-2 focus:ring-[#774A67] focus:shadow-[0_0_15px_#774A67] transition duration-300 disabled:opacity-50 disabled:pointer-events-none "
175211 >
176212 { logging ? < Loader /> : "Sign in" }
177213 </ button >
@@ -184,7 +220,7 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
184220 </ div >
185221
186222 < div className = "text-center" >
187- < p className = "mt-2 text-sm text-gray-600 dark:text-gray-400" >
223+ < p className = "mt-2 text-base text-gray-600 dark:text-gray-400" >
188224 Don't have an account yet?
189225 < a
190226 className = "text-blue-600 decoration-2 hover:underline font-medium dark:focus:outline-none dark:focus:ring-1 dark:focus:ring-gray-600"
@@ -207,7 +243,7 @@ const SignInForm = ({ toggleSignUp, loginSuccess, setLoginSuccess }: Props) => {
207243export default SignInForm ;
208244
209245
210-
246+ // location.replace(redirectUrl); <----- imp do not remove
211247
212248// import { EMAIL_PATTERN } from "@constants/index";
213249// import { ENDPOINTS, fetcher } from "@api/useAxiosSWR";
0 commit comments