delay / flutter_starter

MIT License
422 stars 146 forks source link

Update User Success Notice Bug #21

Closed HussainON closed 2 years ago

HussainON commented 3 years ago

When you update your email with an email that already exists in the database, you get an update user success notice even though the update should fail and the snack bar notice should be "Email is already in use" or something like that.

delay commented 3 years ago

So while you can't actually update to an already existing email to the same address, the firebase api doesn't handle this error correctly and pass it through to us for handling. You can see it has an error message from the command line. However the method channel doesn't get handled and passed along by the firebase_auth api.
`[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: [firebase_auth/email-already-in-use] The email address is already in use by another account.

0 MethodChannelUser.updateEmail (package:firebase_auth_platform_interface/src/method_channel/method_channel_user.dart:195:7)`

Not sure how to fix this at this point without writing a whole bunch of extra code that would try and login with the new email address to see if it exists and then run through the update email function. I don't plan to do that as it would be better for flutter firebase_auth to pass along the error message.

If anyone knows a good way to handle this problem, please let me know otherwise I will close this issue out until the package is updated and correctly handles the error. You may want to pass this problem to the firebase_auth package.

delay commented 3 years ago

It probably would need to be reported here. https://github.com/FirebaseExtended/flutterfire/issues/2582

tezcane commented 2 years ago

I was able to fix it inline with 3 small changes. There is a threading issue. So the error is happening asynchronous to this code so it can't be caught:

         await _auth
            .signInWithEmailAndPassword(email: oldEmail, password: password)
            .then((_firebaseUser) async { // 1. add async here
          await _firebaseUser.user!  // 2. add await here
              .updateEmail(user.email)
              .then((value) => _updateUserFirestore(user, _firebaseUser.user!));
        });
      } catch (err) {
        //not yet working, see this issue https://github.com/delay/flutter_starter/issues/21 (lolz)
        if (err.toString() ==  // 3. I had to modify this if logic too, at least with my firestore lib version.
            "[firebase_auth/email-already-in-use] The email address is already in use by another account.") {
          _authUpdateUserNoticeTitle = 'auth.updateUserEmailInUse'.tr;
          _authUpdateUserNotice = 'auth.updateUserEmailInUse'.tr;
        } else {
delay commented 2 years ago

Thanks so much! I have updated the project.