简体   繁体   中英

How to handle Firebase password reset email errors Flutter

I am making a simple password reset dialog that will send a password reset email to the user via firebase. Everything works fine, however, I want to be able to catch and handle errors accordingly. I can't seem to figure out how to go about doing this. For example, when the user's inte.net connection cuts out, I want them to see a message that their password reset email was not sent via a snackbar.

My Code:

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

You can do this:

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

Read more here .

İt can be late answer but;

With the [documentation][1]

[1]: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/sendPasswordResetEmail.html help, you can catch some use cases, like invalid-continue-uri, missing-ios-bundle-id and etc.

on FirebaseAuthException catch (e) {
 switch(e.code){
   case "missing-ios-bundle-id":
     //do some work
   default:
     ///Show snackbar, navigate user to different page, etc.
}

And also I am highly recomended to create an extension on FirebaseAuthException and with that you can create one of manager function like;

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;
}

with this way(CoreFirebaseAuthExceptionTypes(rename your own error enum)), you can write exception on RenamedYourAuthExceptionTypes and with help of that enum you can simply use useful getters like; CoreFirebaseAuthExceptionTypes.type.snackBarMessage,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM