![](/img/trans.png)
[英]Firebase reset user password in app without email link in flutter?
[英]How to handle Firebase password reset email errors Flutter
我正在制作一个简单的密码重置对话框,它将通过 firebase 向用户发送密码重置 email。一切正常,但是,我希望能够相应地捕获和处理错误。 我似乎无法弄清楚如何 go 这样做。 例如,当用户的 inte.net 连接中断时,我希望他们看到一条消息,表明他们的密码重置 email 不是通过小吃店发送的。
我的代码:
// Send user an email for password reset
Future<void> _resetPassword(String email) async {
await auth.sendPasswordResetEmail(email: email);
}
// This will handle the password reset dialog for login_password
void passwordResetDialog(context, email) {
displayDialog(
context,
title: "Forgot Password?",
content:
"We will send you an email with a password reset link. Press on that link and follow the instructions from there.",
leftOption: "No",
onPressedLeftOption: () {
// Close dialog
Navigator.of(context).pop();
},
rightOption: "Yes",
onPressedRightOption: () {
// Send reset password email
_resetPassword(email);
// Close dialog
Navigator.of(context).pop();
displaySnackBar(
context,
contentText: "Password reset email sent",
durationTime: 7,
);
},
);
}
你可以这样做:
try {
await auth.sendPasswordResetEmail(email: email);
} on FirebaseAuthException catch (e) {
print(e.code);
print(e.message);
// show the snackbar here
}
在这里阅读更多。
这可能是迟到的回答,但是;
随着[文档][1]
[1]: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/sendPasswordResetEmail.html帮助,你可以捕捉到一些用例,比如 invalid-continue-uri,missing-ios-bundle-id 等等.
on FirebaseAuthException catch (e) {
switch(e.code){
case "missing-ios-bundle-id":
//do some work
default:
///Show snackbar, navigate user to different page, etc.
}
而且我也强烈建议在 FirebaseAuthException 上创建一个扩展,你可以创建一个经理 function 之类的;
CoreFirebaseAuthExceptionTypes get coreCreateOperationExceptionType {
switch (code) {
case "email-already-in-use":
return CoreFirebaseAuthExceptionTypes.createUserEmailAlreadyInUse;
case "invalid-email":
return CoreFirebaseAuthExceptionTypes.createUserInvalidEmail;
case "operation-not-allowed":
return CoreFirebaseAuthExceptionTypes.createUserOperationNotAllowed;
case "weak-password":
return CoreFirebaseAuthExceptionTypes.createUserWeakPassword;
default:
return CoreFirebaseAuthExceptionTypes.createUserUnknownError;
}
通过这种方式(CoreFirebaseAuthExceptionTypes(重命名您自己的错误枚举)),您可以在 RenamedYourAuthExceptionTypes 上编写异常,并且借助该枚举,您可以简单地使用有用的吸气剂,例如; CoreFirebaseAuthExceptionTypes.type.snackBarMessage,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.