Add Forgot Password?

This commit is contained in:
MartinBraquet
2025-10-31 20:41:57 +01:00
parent 81e01f1485
commit 0522e787cd
3 changed files with 51 additions and 22 deletions

View File

@@ -0,0 +1,25 @@
import {sendPasswordResetEmail} from "firebase/auth";
import toast from "react-hot-toast";
import {auth} from "web/lib/firebase/users";
export const sendPasswordReset = async (email: string | undefined) => {
if (!email) {
toast.error('No email found on your account.')
return
}
toast.promise(
sendPasswordResetEmail(auth, email),
{
loading: 'Sending password reset email...',
success: 'Password reset email sent — check your inbox and spam.',
error: 'Failed to send password reset email.',
}
)
.catch((e) => {
if (e.code === 'auth/user-not-found') {
toast.error('No account found with that email.')
return
}
console.log("Failed to send password reset email", e)
})
}

View File

@@ -12,11 +12,12 @@ import {useRedirectIfSignedOut} from "web/hooks/use-redirect-if-signed-out";
import {deleteAccount} from "web/lib/util/delete";
import router from "next/router";
import {Button} from "web/components/buttons/button";
import {getAuth, sendEmailVerification, sendPasswordResetEmail, updateEmail} from 'firebase/auth';
import {sendEmailVerification, updateEmail} from 'firebase/auth';
import {auth} from "web/lib/firebase/users";
import {NotificationSettings} from "web/components/notifications";
import ThemeIcon from "web/components/theme-icon";
import {WithPrivateUser} from "web/components/user/with-user";
import {sendPasswordReset} from "web/lib/firebase/password";
export default function NotificationsPage() {
useRedirectIfSignedOut()
@@ -74,25 +75,6 @@ const LoadedGeneralSettings = (props: {
}
}
const sendPasswordReset = async () => {
if (!privateUser?.email) {
toast.error('No email found on your account.')
return
}
const auth = getAuth()
toast.promise(
sendPasswordResetEmail(auth, privateUser.email),
{
loading: 'Sending password reset email...',
success: 'Password reset email sent — check your inbox and spam.',
error: 'Failed to send password reset email.',
}
)
.catch(() => {
console.log("Failed to send password reset email")
})
}
const changeUserEmail = async (newEmail: string) => {
if (!user) return
@@ -195,7 +177,7 @@ const LoadedGeneralSettings = (props: {
<h5>Password</h5>
<Button
onClick={sendPasswordReset}
onClick={() => sendPasswordReset(privateUser?.email)}
className="mb-2"
>
Send password reset email

View File

@@ -8,10 +8,11 @@ import FavIcon from "web/public/FavIcon"
import {signInWithEmailAndPassword} from "firebase/auth"
import {getProfileRow} from "common/profiles/profile"
import {sendPasswordReset} from "web/lib/firebase/password"
import {useUser} from "web/hooks/use-user"
import {db} from "web/lib/supabase/db"
import Router from "next/router"
import {PageBase} from "web/components/page-base"
import {useUser} from "web/hooks/use-user"
import {GoogleButton} from "web/components/buttons/sign-up-button"
import {SEO} from "web/components/SEO"
@@ -162,6 +163,27 @@ function RegisterComponent() {
className="bg-canvas-50 appearance-none rounded-none relative block w-full px-3 py-2 border rounded-b-md border-gray-300 placeholder-gray-500 focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm"
placeholder="Password"
/>
<div className="text-right mt-1 custom-link">
<button
type="button"
onClick={(e) => {
e.preventDefault();
const form = e.currentTarget.closest('form');
if (form) {
const emailInput = form.querySelector('input[type="email"]') as HTMLInputElement;
if (emailInput?.value) {
sendPasswordReset(emailInput.value);
} else {
// If no email is entered, show an error
setError('Please enter your email first');
}
}
}}
className="text-sm focus:outline-none"
>
Forgot password?
</button>
</div>
</div>
</div>