Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.


2 Answers

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.

like image 121
RomJan D. Hossain Avatar answered Nov 10 '25 05:11

RomJan D. Hossain


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

like image 30
Romario Williams Avatar answered Nov 10 '25 05:11

Romario Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!