22 < h2 data-i18n ="auth.forgot_password "> Forgot Password</ h2 >
33 < p data-i18n ="auth.reset_password_email_description "> Enter your email address below and we'll send you a link to reset your password.</ p >
44
5- <!-- Add status message container -->
5+ <!-- Status message container -->
66 < div id ="status-message " class ="status-message " style ="display: none; "> </ div >
77
8- < form id ="reset-password-email- form " method =" post " onsubmit =" return false; ">
8+ < form id ="reset-password-form ">
99 < div class ="form-group ">
1010 < label for ="email " data-i18n ="auth.email "> Email</ label >
1111 < input type ="email " id ="email " name ="email " required >
@@ -39,8 +39,6 @@ <h2 data-i18n="auth.forgot_password">Forgot Password</h2>
3939</ style >
4040
4141< script >
42- console . log ( 'Reset password script loaded' ) ;
43-
4442 // Function to show status message
4543 function showStatusMessage ( message , type ) {
4644 const statusElement = document . getElementById ( 'status-message' ) ;
@@ -54,22 +52,19 @@ <h2 data-i18n="auth.forgot_password">Forgot Password</h2>
5452 statusElement . scrollIntoView ( { behavior : 'smooth' , block : 'center' } ) ;
5553 }
5654
57- // Initialize form immediately
58- function initResetPasswordForm ( ) {
59- console . log ( 'Initializing reset password form' ) ;
60- const form = document . getElementById ( 'reset-password-email-form' ) ;
61- if ( ! form ) {
62- console . error ( 'Reset password form not found' ) ;
63- return ;
64- }
55+ // Initialize form
56+ document . addEventListener ( 'DOMContentLoaded' , ( ) => {
57+ const form = document . getElementById ( 'reset-password-form' ) ;
58+ if ( ! form ) return ;
6559
66- console . log ( 'Adding submit event listener to form' ) ;
6760 form . addEventListener ( 'submit' , async ( e ) => {
68- console . log ( 'Form submit event triggered' ) ;
6961 e . preventDefault ( ) ;
7062
7163 const email = document . getElementById ( 'email' ) . value ;
72- console . log ( 'Email submitted:' , email ) ;
64+ if ( ! email ) {
65+ showStatusMessage ( 'Please enter your email address' , 'error' ) ;
66+ return ;
67+ }
7368
7469 // Show loading state
7570 const submitButton = form . querySelector ( 'button[type="submit"]' ) ;
@@ -78,7 +73,6 @@ <h2 data-i18n="auth.forgot_password">Forgot Password</h2>
7873 submitButton . disabled = true ;
7974
8075 try {
81- console . log ( 'Sending password reset request to server' ) ;
8276 // Send password reset request to the server API
8377 const response = await fetch ( '/api/1/auth/reset-password' , {
8478 method : 'POST' ,
@@ -88,9 +82,7 @@ <h2 data-i18n="auth.forgot_password">Forgot Password</h2>
8882 body : JSON . stringify ( { email } )
8983 } ) ;
9084
91- console . log ( 'Server response status:' , response . status ) ;
9285 const responseData = await response . json ( ) ;
93- console . log ( 'Server response data:' , responseData ) ;
9486
9587 if ( ! response . ok ) {
9688 throw new Error ( responseData . error || response . statusText ) ;
@@ -122,20 +114,74 @@ <h2 data-i18n="auth.forgot_password">Forgot Password</h2>
122114 submitButton . disabled = false ;
123115 }
124116 } ) ;
125- }
126-
127- // Initialize immediately
128- initResetPasswordForm ( ) ;
129-
130- // Also initialize on DOMContentLoaded for safety
131- document . addEventListener ( 'DOMContentLoaded' , ( ) => {
132- console . log ( 'DOMContentLoaded event fired' ) ;
133- initResetPasswordForm ( ) ;
134117 } ) ;
135118
136119 // Also initialize on spa-transition-end event for SPA router
137120 document . addEventListener ( 'spa-transition-end' , ( ) => {
138- console . log ( 'spa-transition-end event fired' ) ;
139- initResetPasswordForm ( ) ;
121+ const form = document . getElementById ( 'reset-password-form' ) ;
122+ if ( ! form ) return ;
123+
124+ // Remove existing event listeners by cloning the form
125+ const newForm = form . cloneNode ( true ) ;
126+ form . parentNode . replaceChild ( newForm , form ) ;
127+
128+ // Add event listener to the new form
129+ newForm . addEventListener ( 'submit' , async ( e ) => {
130+ e . preventDefault ( ) ;
131+
132+ const email = document . getElementById ( 'email' ) . value ;
133+ if ( ! email ) {
134+ showStatusMessage ( 'Please enter your email address' , 'error' ) ;
135+ return ;
136+ }
137+
138+ // Show loading state
139+ const submitButton = newForm . querySelector ( 'button[type="submit"]' ) ;
140+ const originalButtonText = submitButton . textContent ;
141+ submitButton . textContent = window . i18n ? window . i18n . t ( 'auth.sending' ) : 'Sending...' ;
142+ submitButton . disabled = true ;
143+
144+ try {
145+ // Send password reset request to the server API
146+ const response = await fetch ( '/api/1/auth/reset-password' , {
147+ method : 'POST' ,
148+ headers : {
149+ 'Content-Type' : 'application/json'
150+ } ,
151+ body : JSON . stringify ( { email } )
152+ } ) ;
153+
154+ const responseData = await response . json ( ) ;
155+
156+ if ( ! response . ok ) {
157+ throw new Error ( responseData . error || response . statusText ) ;
158+ }
159+
160+ // Show success message
161+ const successMessage = window . i18n ? window . i18n . t ( 'auth.reset_email_sent' ) : 'Password reset link has been sent to your email. Please check your inbox.' ;
162+ showStatusMessage ( successMessage , 'success' ) ;
163+
164+ // Clear the form
165+ newForm . reset ( ) ;
166+ } catch ( error ) {
167+ console . error ( 'Password reset email error:' , error ) ;
168+
169+ // Show error message
170+ let errorMessage = window . i18n ? window . i18n . t ( 'errors.reset_email_failed' ) : 'Failed to send password reset email' ;
171+
172+ // Check if the error is about email not found
173+ if ( error . message && ( error . message . includes ( 'not found' ) || error . message . includes ( 'not exist' ) ) ) {
174+ errorMessage = window . i18n ? window . i18n . t ( 'errors.email_not_found' ) : 'Email address not found. Please check your email and try again.' ;
175+ } else {
176+ errorMessage += ': ' + ( error . message || ( window . i18n ? window . i18n . t ( 'errors.unknown_error' ) : 'An unknown error occurred' ) ) ;
177+ }
178+
179+ showStatusMessage ( errorMessage , 'error' ) ;
180+ } finally {
181+ // Reset button state
182+ submitButton . textContent = originalButtonText ;
183+ submitButton . disabled = false ;
184+ }
185+ } ) ;
140186 } ) ;
141187</ script >
0 commit comments