1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Firebase signInWithEmailAndPassword() failing to catch "PlatformException"...

Discussão em 'Mobile' iniciado por Stack, Outubro 30, 2024 às 19:42.

  1. Stack

    Stack Membro Participativo

    I am a beginner trying to learn mobile app development for both Android and IOS using Flutter. I am trying to implement simple user authentication using Firebase. I have a standard login screen with text fields and a button which has an OnTap widget that will execute the signUserIn() method, shown below. Here is the method I am executing:

    final emailController = TextEditingController();
    final passwordController = TextEditingController();
    void signUserIn() async {
    try { await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: emailController.text,
    password: passwordController.text,
    );
    } on PlatformException catch (e) {
    if (e.code == 'ERROR_INVALID_CREDENTIALS') {
    wrongEmailMessage();
    }


    I am certain that the try block is being executed properly, because when I input valid user credentials, the rest of my program executes as I expect it to. However, when I purposefully enter invalid credentials, I am thrown the following message from the messages.pigeon.dart file, which I believe is part of the import package:firebase_auth/firebase_auth.dart';

    Exception has occurred. PlatformException (PlatformException(ERROR_INVALID_CREDENTIAL, The supplied auth credential is incorrect, malformed or has expired., {}, null))

    However, I also see on the debug console:

    E/RecaptchaCallWrapper(13275): Initial task failed for action RecaptchaAction(action=signInWithPassword)with exception - The supplied auth credential is incorrect, malformed or has expired.

    There is a post I saw which explained that the setting to protect against email enumeration attacks is enabled by default, which hides codes like 'user-not-found' and 'wrong-password' response codes which are given by FirebaseAuthException. The person suggested either turning it off or "handle the new, more generic error code you are getting" without any instruction on how to do this, I just tried my best to catch the exception a different way.

    I have tried many different variations of the part after the try block. Some of them include:

    } on FirebaseAuthException {
    } on PlatformException {

    } on Exception catch (e) {
    if (e.code == 'ERROR_INVALID_CREDENTIAL')
    // do whatever

    } on Error catch (e) {
    if (e.code == 'ERROR_INVALID_CREDENTIAL')
    // do whatever


    I've also just tried to catch anything by doing something like

    } on Error catch (_) } { or } on Exception catch (_) {

    I'm truthfully quite confused as to why I can't seem to catch errors at all. This leads me to believe that my conceptual understanding of try / on / catch is incorrect, but when I look at documentation on how to structure it syntactically, everything is correct. Is it because the message is being thrown internally from the imported package? I'm referencing the exact code that they provide when I hover over the platform exception thrown from this messages.pigeon.dart file though. And even if I was using the wrong exception or error code, shouldn't I be able to catch literally any error that comes? Or is it because it isn't an error through flutter but through firebase?

    I was really hoping I could get past this part without having to ask a customized question, but unfortunately all documentation I've seen on this issue just points to disabling protections and I don't want to do that. I want to have the application be secure against known attacks, and I wanted to overcome this myself but I just can't stand to see one more freaking Platform Exception!

    Any help or insight you could provide would be great! If you need any additional information, please let me know.

    Continue reading...

Compartilhe esta Página