我正在使用 React Native 版本 0.72 和 Expo 版本 49.0.15,并且我有一个可以处理密码更改的屏幕,但是当用户尝试更新密码时,应用程序会自行关闭。
在屏幕上,我有 3 个输入,用户可以输入旧密码、新密码和重复新密码。
单击按钮时,将调用一个函数来处理密码更改。这是我的代码:
const [password, setPassword] = useState('');
const [newPassword, setNewPassword] = useState('');
const savePassword = () => {
const creds = EmailAuthProvider.credential(email, password);
auth().currentUser.reauthenticateWithCredential(creds)
.then(() => {
auth().currentUser.updatePassword(newPassword)
.then(() => {
console.log('password successfully updated.');
});
});
}
在模拟器中,“密码已成功更新。”出现,所以我相信这意味着密码已更改。但是当我尝试使用新密码重新登录时,它不起作用,旧密码可以。
在实际设备中,应用程序只是自行退出,并没有更改密码。
尝试使用
await
const [password, setPassword] = useState('');
const [newPassword, setNewPassword] = useState('');
const savePassword = async () => {
const creds = EmailAuthProvider.credential(email, password);
const reAuthResult =await auth().currentUser.reauthenticateWithCredential(creds)
.then(async () => {
await auth().currentUser.updatePassword(newPassword)
.then(() => {
console.log('password successfully updated.');
});
});
}
请告诉我任何信息
第二个方法
const [password, setPassword] = useState('');
const [newPassword, setNewPassword] = useState('');
const savePassword = async () => {
try {
const creds = EmailAuthProvider.credential(email, password);
await auth().currentUser.reauthenticateWithCredential(creds);
await auth().currentUser.updatePassword(newPassword);
console.log('Password successfully updated.');
} catch (error) {
console.log('Error updating password:', error);
}
};