So I'm trying to make a simple app using Flutter, where I'm using Firebase to authenticate users' Emails and Passwords to sign into the app.
Where it's going wrong is that users can sign in - which indicates to me that Firebase is working - but when I use a try catch statement where I tell the user if they input their email or password wrong, it doesn't react at all.
I have import 'package:firebase_auth/firebase_auth.dart';
imported
Here is the signUserIn
function:
class _LoginPageState extends State<LoginPage> {
// text editing controllers
final emailController = TextEditingController();
final passwordController = TextEditingController();
// sign user in method
void signUserIn() async {
// show loading circle
showDialog(
context: context,
builder: (context) {
return const Center(
child: CircularProgressIndicator(),
);
},
);
// try sign in
try {
final credential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
// pop the loading circle
Navigator.pop(context);
} on FirebaseAuthException catch (e) {
// pop the loading circle
Navigator.pop(context);
// Incorrect e-mail
if (e.code == 'user-not-found') {
// show error to user
wrongEmailMessage();
}
// Incorrect password
else if (e.code == 'wrong-password') {
// show error to user
wrongPasswordMessage();
}
}
}
This is the wrongEmailMessage()
function:
void wrongEmailMessage() {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
backgroundColor: Colors.deepPurple,
title: Center(
child: Text(
'Incorrect Email',
style: TextStyle(color: Colors.white),
),
),
);
},
);
}
Here is where I call it - on button press:
SignInBlock(
onTap: signUserIn,
),
I called the wrongEmailMessage()
function in my SignInBlock
and it did work, so my only theory is something to do with Firebase isn't right.
Are there some common issues that I'm missing to spot? If any more information is needed I will provide it.
I've tried just printing to the console instead of calling the function still didn't work. I have tried using different e.code == "..."
with no avail. When I click the SignInBlock
button, it works when I have the information written correctly, but when I don't, the try block doesn't catch it.
On Firebase projects created since September 15, email enumeration protection is enabled by default. While this protects your users from abuse, the API doesn't distinguish between user-not-found
and wrong-password
anymore and instead throws a more generic error that is less susceptible to abuse.
You can either update the code to handle the more vague error codes, or you can disable email enumeration protection to get back the old behavior.
Also see my answer here: Firebase Authentication Error Handling in iOS Apps and I Don't get Error codes of Firebase Auth in my Flutter App (I ONLY get INVALID_LOGIN_CREDENTIALS)