0

I'm making a Flutter application with FirebaseAuth and I want to custom the authentification error messages when the user logs in the app.

Here's the login function :

  Future loginOlimUser(String email, password) async {
try {
  User user = (await _auth.signInWithEmailAndPassword(
          email: email, password: password))
      .user!;

} on FirebaseAuthException catch (e) {
  return e.message;
}

}

I call this function into a button in the Login page :

TextButton(
                onPressed: () async {
                  if (_key.currentState!.validate()) {
                    setState(() => loading = true);

                    var email = _emailController.text;

                    var password = _passwordController.text;

                    await _auth
                        .loginOlimUser(email, password)
                        .then((value) async {
                      if (value == true) {
                        Navigator.pushNamed(context, Homepage.id);
                      } else {
                        mySnackBar(context, Colors.red, value);
                        setState(() {
                          loading = false;
                        });
                      }
                    });
                  }
                },
              ),

So basically when the user logs in , he's pushed to the Homepage, but if there's any exception, I managed to display the error message in a Snackbar :

void mySnackBar(context, color, message) {
ScaffoldMessenger.of(context).showSnackBar(
  SnackBar(
    content: Text(
      message,
    ),
    backgroundColor: color,
    duration: Duration(seconds: 2),
    action: SnackBarAction(
      label: "OK",
      onPressed: () {},
      textColor: Colors.white,
    ),
  ),
);

}

But the problem is that it displays the default firebaseAuth error message and I want to custom these messages.

I've been searching a lot, but I couldn't make work any of the solutions I found.

All I have is this codes for Flutter Firebase Auth Exception but I really don't understand how to use them.

leuji
  • 41
  • 5
  • 1
    This answer to the question you linked seems exactly what you need: https://stackoverflow.com/a/70870434. If you want to handle the specific code also linked there, have a look at this video: https://www.youtube.com/watch?v=Gz0x77Hd1GE It shows how to get the `e.code` around the 45m mark. – Frank van Puffelen Nov 09 '22 at 14:49
  • Check this out, if you want custom error classes https://github.com/SankethBK/diaryaholic/blob/master/lib/features/auth/data/repositories/authentication_repository.dart – Sanketh B. K Nov 09 '22 at 15:05

1 Answers1

0

If you want to customize the message displayed to your user, you'll want to switch to using e.code in your code.

try {
  var result = await _auth.signInWithEmailAndPassword(
          email: email, password: password)
  User user = result.user!;
} on FirebaseAuthException catch (e) {
  return e.code; // 
}

For a full list of error codes, see Where can I find a list of all error codes and messages for firebase authentication API.

I also recommend watching this (long) video Firebase Authentication in Flutter (The Boring Flutter Development Show, Ep. 55).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807