我写了一个这样的代码:
TextFormField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
),
),
// ...
FirebaseAuth.instance.currentUser!.updatePassword(_passwordController.text).then((value) {
}).catchError((e) {
ModernAlert(
context,
"Hata",
"Bir hata meydana geldi. Lütfen daha sonra tekrar deneyin.",
"Tamam",
() {},
);
});
我的目标是用TextFormField
从用户那里获得新密码,并在Firebase Auth系统中更改他的密码。但我得到了这个错误:
FirebaseAuthException ([firebase_auth/user-token-expired] The user's credential is no longer valid. The user must sign in again.)
是什么原因导致了这个问题?我该如何解决?
此错误是因为用户Unauthentication,并读取此文档FirebaseAuthInvalidUserException
解决问题添加此功能:
Future<bool> _changePassword(String currentPassword, String newPassword)
async {
bool success = false;
//Create an instance of the current user.
var user = await FirebaseAuth.instance.currentUser!;
//Must re-authenticate user before updating the password. Otherwise it may
fail or user get signed out.
final cred = await EmailAuthProvider.credential(email: user.email!,
password: currentPassword);
await user.reauthenticateWithCredential(cred).then((value) async {
await user.updatePassword(newPassword).then((_) {
success = true;
}).catchError((error) {
print(error);
});
}).catchError((err) {
print(err);
});
return success;
}