Skip to content

Commit 22f0fb0

Browse files
committed
Implement production-ready forgot password with proper email validation
1 parent 4b40387 commit 22f0fb0

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/app/auth/forgot-password/page.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useState } from "react";
4-
import { resetPassword } from "@/lib/firebase/auth";
4+
import { resetPassword, checkEmailExists } from "@/lib/firebase/auth";
55
import Link from "next/link";
66
import { Mail, ArrowLeft } from "lucide-react";
77
import { useToast } from "@/context/ToastContext";
@@ -18,21 +18,28 @@ export default function ForgotPasswordPage() {
1818

1919
// Validate email domain
2020
if (!isAllowedEmail(email)) {
21-
addToast("Can't Access SORRY", "error");
21+
addToast("Please enter a valid email address.", "error");
2222
setLoading(false);
2323
return;
2424
}
2525

2626
try {
27-
// Send password reset email
28-
// Note: Firebase won't error for non-existent emails (security feature)
27+
// Check if email exists in Firebase Auth
28+
const emailExists = await checkEmailExists(email);
29+
30+
if (!emailExists) {
31+
addToast("We couldn't find an account with this email address.", "error");
32+
setLoading(false);
33+
return;
34+
}
35+
36+
// Email exists - send password reset link
2937
await resetPassword(email);
30-
addToast("If an account exists with this email, a password reset link has been sent.", "success");
31-
// Clear the email field after success
32-
setEmail("");
38+
addToast("Password reset link has been sent to your email.", "success");
39+
setEmail(""); // Clear the email field
3340
} catch (err: any) {
3441
console.error("Error in handleReset:", err);
35-
addToast("Can't reset password. Please try again.", "error");
42+
addToast("Something went wrong. Please try again later.", "error");
3643
} finally {
3744
setLoading(false);
3845
}

src/lib/firebase/auth.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,14 @@ export const signOut = () => firebaseSignOut(auth);
6464
export const checkEmailExists = async (email: string): Promise<boolean> => {
6565
try {
6666
const methods = await fetchSignInMethodsForEmail(auth, email);
67-
console.log(`Email ${email} check - Sign-in methods:`, methods);
68-
const exists = methods.length > 0;
69-
console.log(`Email ${email} exists:`, exists);
70-
return exists;
67+
return methods.length > 0;
7168
} catch (error: any) {
72-
console.error("Error checking email:", error);
7369
// If error code is auth/invalid-email, the email format is wrong
7470
if (error.code === 'auth/invalid-email') {
7571
return false;
7672
}
77-
// For other errors, assume email doesn't exist to be safe
73+
// For other errors, return false to be safe
74+
console.error("Error checking email existence:", error);
7875
return false;
7976
}
8077
};

0 commit comments

Comments
 (0)