如何解决使用本机反应更改 firebase 中的密码
我正在尝试使用 react native 更新数据库 firebase 中的密码 它有效,但我有错误“密码必须为 6 个字符长或更多”
我知道这是因为“this.setState({newPassword: "",currentPassword: ""})”,但我不知道如何解决它..
export default class ChangePassword extends React.Component {
constructor(props) {
super(props);
this.state = {
newPassword: "",currentPassword: "",};
}
reauthenticate = (currentPassword) => {
console.log("currentPassword = "+currentPassword)
var user = firebase.auth().currentUser;
var cred = firebase.auth.EmailAuthProvider.credential(
user.email,currentPassword
);
return user.reauthenticateWithCredential(cred);
};
OnChangePassword = async () => {
if (this.state.currentPassword === "" || this.state.newPassword === "") {
alert("All the fields are required");
}
else if( this.state.newPassword.length <= 5)
{
alert("Password must contains more than 5 characters");
} else {
try {
await this.reauthenticate(this.state.currentPassword).then(() => {
var user = firebase.auth().currentUser;
user.updatePassword(this.state.newPassword);
const response = firebase.database().ref("Users").child(user.uid);
this.setState({
currentPassword:"",newPassword:""
})
})
} catch (error) {
console.log(error);
alert(error);
}
}
};
render() {
return (
<View >
<TextInput
secureTextEntry={true}
onChangeText={(currentPassword) =>
this.setState({ currentPassword })
}
/>
<TextInput
secureTextEntry={true}
onChangeText={(newPassword)=>this.setState({newPassword})}
/>
<Button onPress={() => this.OnChangePassword()}/>
</View>
);
}
}
}
解决方法
使用 formik 或任何其他 formLibrary,您可以在其中设置密码长度,您可以通过该长度来验证您想要的最小和最大字符数,并使用用户输入的密码验证它,方法是使用您的代码解决此问题,如果条件..
if (this.state.newPassword.length <= 5 ) {
Alert("New Password Should Contain more than 5 characters")
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。