I’m adding password reset function using react native expo and firebase api.
First input is current password.
Second input is new password.
And third input is confirm new password.
To check the current password, I have to get current password.
Please help me!
Advertisement
Answer
You can use reauthenticateWithCredential()
to verify user’s password and then updatePassword()
to update it:
JavaScript
x
21
21
1
import { getAuth, reauthenticateWithCredential, EmailAuthProvider } from "firebase/auth";
2
3
const auth = getAuth();
4
5
const resetUserPassword = async () => {
6
const user = auth.currentUser;
7
8
const cred = EmailAuthProvider.credential(user.email, "[USER_PASSWORD]");
9
10
try {
11
await reauthenticateWithCredential(user, cred)
12
13
// User entered correct credentials
14
// Update password
15
await updatePassword(auth.currentUser, "[NEW_PASSWORD]");
16
} catch (e) {
17
console.log(e.code, e.message)
18
// Could be incorrect credentials
19
}
20
}
21
Updating password requires a recent login re-authentication is necessary if user is logged in for long time.