Home > OS >  When I Try To Handle an Authentication Exception in Firebase it Doesn't Work
When I Try To Handle an Authentication Exception in Firebase it Doesn't Work

Time:10-23

I'm trying to handle Authentication exceptions in Firebase such as user-not-found or wrong-password. I was doing it by using switch-case and it was working well yesterday, but suddenly it stopped working today for no any specific reason.

onPressed: () async {
        var user = await auth.signInWithEmailAndPassword(
            email: emailController.text, password: passwordController.text);

        

        try {
          if (await user !=
              null) {
            Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => HomeScreen(),
                ),
                (route) => false);
          }
        } on FirebaseAuthException catch (e) {
          print(e.message);
          
          switch (e.code) {
            case "wrong-password":
              return callSnackbar("Wrong password.");
            case "user-not-found":
              return callSnackbar("Wrong E Mail");
            case "too-many-requests":
              return callSnackbar("Please try a few seconds later");
            case "invalid-email":
              return callSnackbar("Account not found");
            default:
          }
        }
      },

When I enter the wrong e mail the error that I get is:

  FirebaseAuthException ([firebase_auth/user-not-found] There is no user record corresponding to this identifier. The user may have been deleted.)

CodePudding user response:

Move this to be inside your try:

var user = await auth.signInWithEmailAndPassword(
            email: emailController.text, password: passwordController.text);

and your if statement on the user shouldn't have an await.

  • Related