11import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-app.js" ;
2- import { getAuth , GoogleAuthProvider , signInWithPopup } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-auth.js" ;
2+ import { getAuth , GoogleAuthProvider , signInWithPopup , sendPasswordResetEmail } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-auth.js" ;
33
44// Secure Firebase configuration - fetched from server
55let firebaseConfig = null ;
@@ -34,7 +34,7 @@ async function initializeFirebase() {
3434
3535const provider = new GoogleAuthProvider ( ) ;
3636
37- // Initialize Firebase and set up event listeners
37+ // Handle Google Login and Password Reset
3838async function setupFirebaseAuth ( ) {
3939 const initialized = await initializeFirebase ( ) ;
4040 if ( ! initialized ) {
@@ -44,7 +44,7 @@ async function setupFirebaseAuth() {
4444
4545 const googleLogin = document . getElementById ( "google-login-btn" ) ;
4646 if ( googleLogin ) {
47- googleLogin . addEventListener ( "click" , async function ( ) {
47+ googleLogin . addEventListener ( "click" , async function ( ) {
4848 try {
4949 const result = await signInWithPopup ( auth , provider ) ;
5050 const credential = GoogleAuthProvider . credentialFromResult ( result ) ;
@@ -77,8 +77,59 @@ async function setupFirebaseAuth() {
7777 }
7878 } ) ;
7979 }
80+
81+ // Handle forgot password form submission
82+ const forgotPasswordForm = document . getElementById ( "forgot-password-form" ) ;
83+ if ( forgotPasswordForm ) {
84+ forgotPasswordForm . addEventListener ( "submit" , async ( event ) => {
85+ event . preventDefault ( ) ;
86+ const emailInput = document . getElementById ( 'email' ) ;
87+ const resetBtn = document . getElementById ( 'reset-btn' ) ;
88+ const resetText = document . getElementById ( 'reset-text' ) ;
89+
90+ const email = emailInput . value . trim ( ) ;
91+ if ( ! email ) {
92+ alert ( "Please enter a valid email address." ) ;
93+ return ;
94+ }
95+
96+ // Add loading state
97+ resetBtn . classList . add ( 'loading' ) ;
98+ resetText . textContent = 'Sending...' ;
99+ resetBtn . disabled = true ;
100+
101+ try {
102+ await sendPasswordResetEmail ( auth , email ) ;
103+ alert ( "A password reset link has been sent to your email. Please check your inbox." ) ;
104+ // Redirect to login page after a short delay
105+ setTimeout ( ( ) => {
106+ window . location . href = 'login.html' ;
107+ } , 3000 ) ;
108+ } catch ( error ) {
109+ const errorCode = error . code ;
110+ const errorMessage = error . message ;
111+ console . error ( "Password reset error:" , errorCode , errorMessage ) ;
112+
113+ // Show user-friendly error message
114+ switch ( errorCode ) {
115+ case 'auth/invalid-email' :
116+ alert ( "The email address is not valid." ) ;
117+ break ;
118+ case 'auth/user-not-found' :
119+ alert ( "No account found with this email address." ) ;
120+ break ;
121+ default :
122+ alert ( "Failed to send password reset email. Please try again." ) ;
123+ break ;
124+ }
125+
126+ resetBtn . classList . remove ( 'loading' ) ;
127+ resetText . textContent = 'Reset Password' ;
128+ resetBtn . disabled = false ;
129+ }
130+ } ) ;
131+ }
80132}
81133
82134// Initialize Firebase authentication when the script loads
83- setupFirebaseAuth ( ) ;
84- } ) ;
135+ setupFirebaseAuth ( ) ;
0 commit comments