dart 如何处理Firebase密码重置电子邮件错误Flutter

a7qyws3x  于 2023-01-03  发布在  Flutter
关注(0)|答案(2)|浏览(135)

我正在做一个简单的密码重置对话框,它将通过firebase向用户发送一封密码重置邮件。一切正常,但是,我希望能够捕捉并相应地处理错误。我似乎不知道如何去做这件事。例如,当用户的互联网连接切断时,我希望他们看到一条消息,他们的密码重置邮件不是通过snackbar发送的。
我的代码:

// 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,
      );
    },
  );
}
fhity93d

fhity93d1#

您可以执行以下操作:

try {
 await auth.sendPasswordResetEmail(email: email);
} on FirebaseAuthException catch (e) {
 print(e.code);
 print(e.message);
// show the snackbar here
}

了解更多here.

iqjalb3h

iqjalb3h2#

它可以迟回答,但是;
与[文档][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上创建一个扩展,这样你就可以创建一个管理器函数,比如;

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上编写异常,且在该枚举的帮助下,您可以简单地使用有用的getter,例如;核心FirebaseAuthExceptionTypes.类型.零食条消息,

相关问题