我想更改firebase的当前密码。但是,如何使用旧密码使用颤振验证当前密码?
我的change_password.dart表单文件
Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _oldPasswordController,
obscureText: hidePassword,
decoration: InputDecoration(
labelText: AppString.oldPassword,
),
validator: (value) {
if (value!.isEmpty) {
return AppString.enterPassword;
} else if (value.length <= 5) {
return AppString.passwordValidation;
}
return null;
},
),
TextFormField(
controller: _changePasswordController,
obscureText: hidePassword,
decoration: InputDecoration(
labelText: AppString.newPassword,
),
validator: (value) {
if (value!.isEmpty) {
return AppString.enterPassword;
} else if (value.length <= 5) {
return AppString.passwordValidation;
}
return null;
},
),
Row(
children: [
child: ElevatedButton(
style: buttonStyle,
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
changePassword(
_changePasswordController!.text,
);
}
},
child: Text(
AppString.changePassword,
),
),更改密码函数,其中从小部件:调用函数
void changePassword(String password) async {
try {
await currentUser!.updatePassword(password);
AuthProvider.logOut().whenComplete(() {
snackBarWidgets(context, AppString.pswdChangedSuccessful);
});
} catch (e) {
rethrow;
}
}图像

发布于 2022-01-20 06:52:56
在调用updatePassword之前添加以下行
final user = FirebaseAuth.instance.currentUser;
// you should check here that email is not empty
final credential = EmailAuthProvider.credential(
email: user.email!,
password: <password>);
try {
await user.reauthenticateWithCredential(credential);
// proceed with password change
} on FirebaseAuthException catch (e) {
// handle if reauthenticatation was not successful
}https://stackoverflow.com/questions/70776144
复制相似问题