3

I'm working on a firebase app that contains an authentication system that allows users to sign up/login and create a profile. The app stores and calls profile info about the users. The system worked just fine until I realized there's no way for a user to reset their password. So I added sendPasswordResetEmail function and it's been downhill since then. I'm a newbie to firebase and to StackOverflow so forgive the mess you're about to see. Am I missing something in that function?

const resetpassword = async () => { const email = document.getElementById('email').value; try { const { user } = auth.sendPasswordResetEmail(email); alert('Password Reset Email Sent!'); } catch(error) { console.log("error ===>", error); if (error.message === "Firebase: Error (auth/user-not-found).") { alert("There is no user corresponding to this email address.")} else (errorCode == 'auth/invalid-email') { alert(errorMessage)} } } 

3 Answers 3

9

If you are using firebase version 9 then use code snippet below to reset user password

import { getAuth, sendPasswordResetEmail } from "firebase/auth"; const auth = getAuth(); sendPasswordResetEmail(auth, email) .then(() => { // Password reset email sent! // .. }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; // .. });

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. I'm using firebase 9. When I ran your fix I got the same results. The whole app work until I add the sendpasswordresetemail.
How do we handle the flow after the user clicks the link in the email? I don't find any info in the docs for this. How do we actually change the password ?!
0

If you want to handle this inside you app, a solution is given here: https://stackoverflow.com/a/37955339/22991834

You need to configure the reset password template inside the Firebase console.

Comments

0

In case you want user to update their password without sending any email, you can use this code below

 import { getAuth, updatePassword } from "firebase/auth"; const user = auth.currentUser; updatePassword(user, password).then(() => { // Update successful. }) 

1 Comment

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.