Skip to content

Commit 4496bfa

Browse files
authored
Merge pull request #202 from vakrahul/my_contribution
Reset password funtionality
2 parents 648d11a + a964495 commit 4496bfa

2 files changed

Lines changed: 58 additions & 49 deletions

File tree

firebase.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { 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
55
let firebaseConfig = null;
@@ -34,7 +34,7 @@ async function initializeFirebase() {
3434

3535
const provider = new GoogleAuthProvider();
3636

37-
// Initialize Firebase and set up event listeners
37+
// Handle Google Login and Password Reset
3838
async 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();

forgot-password.html

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h2>Reset Password</h2>
2727
<p class="form-subtitle">Enter your email to reset your password</p>
2828
</div>
2929

30-
<form onsubmit="handleForgotPassword(event)">
30+
<form id="forgot-password-form">
3131
<div class="input-group">
3232
<i class="fas fa-envelope"></i>
3333
<input type="email" id="email" placeholder="Enter your email" required>
@@ -40,49 +40,7 @@ <h2>Reset Password</h2>
4040
<p>Remember your password? <a href="login.html">Sign In</a></p>
4141
</form>
4242
</div>
43-
44-
<script src="auth.js"></script>
4543
<script src="theme.js"></script>
46-
<script>
47-
function handleForgotPassword(event) {
48-
event.preventDefault();
49-
50-
const resetBtn = document.getElementById('reset-btn');
51-
const resetText = document.getElementById('reset-text');
52-
const emailInput = document.getElementById('email');
53-
54-
// Add loading state
55-
resetBtn.classList.add('loading');
56-
resetText.textContent = 'Processing...';
57-
resetBtn.disabled = true;
58-
59-
// Reset previous states
60-
emailInput.classList.remove('error', 'success');
61-
62-
const email = emailInput.value.trim();
63-
64-
setTimeout(() => {
65-
const result = window.authManager.resetPassword(email);
66-
67-
if (result.success) {
68-
emailInput.classList.add('success');
69-
resetText.textContent = 'Password Reset!';
70-
71-
// Show temporary password (in production, this would be sent via email)
72-
showAuthMessage(`Password reset successful! Your temporary password is: ${result.tempPassword}`, 'success');
73-
74-
setTimeout(() => {
75-
window.location.href = 'login.html';
76-
}, 5000);
77-
} else {
78-
showAuthMessage(result.message, 'error');
79-
emailInput.classList.add('error');
80-
resetBtn.classList.remove('loading');
81-
resetText.textContent = 'Reset Password';
82-
resetBtn.disabled = false;
83-
}
84-
}, 1500);
85-
}
86-
</script>
44+
<script type="module" src="firebase.js"></script>
8745
</body>
8846
</html>

0 commit comments

Comments
 (0)