Flutter FirebaseAuth: SignInWithEmailAndPassword unable to handle error when the email address is badly formatted

2,021

Solution 1

check this issues where it explains why is happens https://github.com/FirebaseExtended/flutterfire/issues/1760

. If you use an Emulator and change textfield with tab button then there's an extra space left behind on that email field ->*[email protected] *

to avoid this extra space you have to use .trim() method.

in your case

UserCredential _signInWithEmailAndPasswordGoogle = await _auth
          .signInWithEmailAndPassword(email: emailAddress.trim(), password: password);

I guess this solves your problem.

Solution 2

I was having a similar issue, your code looks fine. Try disabling Uncaught Expectations in the Debugging panel for VScode solved the issue for me. https://github.com/FirebaseExtended/flutterfire/issues/3303#issuecomment-687560133

Share:
2,021
Admin
Author by

Admin

Updated on December 23, 2022

Comments

  • Admin
    Admin over 1 year

    Here is my email sign in method in my FirebaseAuthService class:

      @override
      Future<UserCustom> signInWithEmail(
          String emailAddress, String password) async {
        try {
          UserCredential _signInWithEmailAndPasswordGoogle = await _auth
              .signInWithEmailAndPassword(email: emailAddress, password: password);
          if (_signInWithEmailAndPasswordGoogle.user != null) {
            return _userToUserModel(_signInWithEmailAndPasswordGoogle.user);
          } else {
            throw PlatformException(
                code: 'SIGN_IN_INTERRUPTED', message: 'Sin in interrupted');
          }
        } on PlatformException {
          print('Happened');
          rethrow;
        } 
      }
    

    And here is where the exception should be handled:

      // creating the submit function
      Future<void> _submit(EmailSignInModelProviderPattern model) async {
        // if it is on sign in use sign in function ELSE use register function
        try {
          await model.submit();
          Navigator.pop(context);
        } on PlatformException catch (e) {
          CustomErrorPlatformException(
            title: 'Sign in failed',
            exception: e,
          ).show(context);
        } catch(e){
          print(e.toString());
        }
      }
    

    And yet when I enter a badly formatted address the process is interrupted at message_codecs.dart file at the method dynamic decodeEnvelope(ByteData envelope){... line 572 with error message: See screenshot

    Exception has occurred. PlatformException (PlatformException(firebase_auth, com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted., {code: invalid-email, additionalData: {}, message: The email address is badly formatted.}))

    I couldn't figure out how to handle this exception, knowing that it never happened to me before upgrading to firebase_auth: ^0.18.0+1.