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.