class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
/* -- login screen text controllers -- */
final emailController = TextEditingController();
final passwordController = TextEditingController();
/* -- login screen text controllers -- */
/* -- login user in method -- */
void loginUser() async {
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found' || e.code == 'invalid-email') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Theme.of(context).colorScheme.secondary,
content: Text(
tEmailIncorrect,
style: Theme.of(context).textTheme.bodyText1,
),
),
);
} else if (e.code == 'wrong-password') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Theme.of(context).colorScheme.secondary,
content: Text(
tPasswordIncorrect,
style: Theme.of(context).textTheme.bodyText1,
),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Theme.of(context).colorScheme.secondary,
content: Text(
tEmailAndPasswordIncorrect, // Use the generic error message here
style: Theme.of(context).textTheme.bodyText1,
),
),
);
}
}
}
/* -- login user in method -- */
@override
Widget build(BuildContext context) {
return Scaffold(
/* -- Body -- */
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
height: 50.0,
),
/* -- Logo -- */
Icon(
CupertinoIcons.lock_fill,
color: Theme.of(context).iconTheme.color,
size: 100.0,
),
/* -- Logo -- */
const SizedBox(
height: 50.0,
),
/* -- Welcome back, you have been missed -- */
Text(
tWelcomeBackYouHaveBeenMissed,
style: Theme.of(context).textTheme.bodyText1,
),
/* -- Welcome back, you have been missed -- */
const SizedBox(
height: 25.0,
),
/* -- Username textformfield -- */
MyTextFormField(
controller: emailController,
hintText: tEmail,
obscureText: false,
),
/* -- Username textformfield -- */
const SizedBox(
height: 10.0,
),
/* -- Password textformfield -- */
MyTextFormField(
controller: passwordController,
hintText: tPassword,
obscureText: true,
),
/* -- Password textformfield -- */
const SizedBox(
height: 10.0,
),
/* -- Forgot password -- */
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
tForgotPassword,
style: TextStyle(
color: Theme.of(context).textTheme.bodyText1!.color,
fontWeight: FontWeight.bold,
),
),
],
),
),
/* -- Forgot password -- */
const SizedBox(
height: 25.0,
),
/* -- Login button -- */
MyCustomButton(
buttonBackgroundColor: cBlueColor,
buttonText: tLogin,
buttonTextColor: cWhiteTextColor,
onTap: loginUser,
),
/* -- Login button -- */
const SizedBox(
height: 50.0,
),
/* -- Or continue with -- */
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
children: [
const Expanded(
child: Divider(),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
tOrContinueWith,
style: Theme.of(context).textTheme.bodyText1,
),
),
const Expanded(
child: Divider(),
),
],
),
),
/* -- Or continue with -- */
const SizedBox(
height: 25.0,
),
/* -- Google + facebook + apple + twitter login buttons -- */
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// google
SquareTile(imagePath: iGoogleLogoImage),
SizedBox(
width: 10.0,
),
// facebook
SquareTile(imagePath: iFacebookLogoImage),
SizedBox(
width: 10.0,
),
// apple
SquareTile(imagePath: iAppleLogoImage),
SizedBox(
width: 10.0,
),
// x
SquareTile(imagePath: iXLogoImage),
],
),
/* -- Google + facebook + apple + twitter login buttons -- */
const SizedBox(
height: 50.0,
),
/* -- Don't you have an account? + register now text -- */
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
tDontYouHaveAnAccount,
style: Theme.of(context).textTheme.bodyText1,
),
const SizedBox(
width: 10.0,
),
Text(
tRegisterNow,
style: Theme.of(context).textTheme.headline5,
),
],
),
/* -- Don't you have an account? + register now text -- */
],
),
),
),
),
/* -- Body -- */
);
}
}
我面临的问题是:
我开发了一个用户登录表单。但是,即使只有电子邮件部分输入错误,我也会收到消息“您的电子邮件地址和密码都不正确”。当电子邮件输入错误时,我想收到一条消息,表明只有电子邮件地址不正确。
我一直在等待的解决方案:
当电子邮件地址输入错误时,仅应向用户显示电子邮件地址不正确的错误消息。同样,如果密码输入错误,则仅显示密码错误消息。
发生的事件:
目前,即使只有电子邮件地址输入错误,也会显示“您的电子邮件地址和密码均不正确”的消息。
对于 2023 年 9 月以来创建的项目,Firebase 会自动启用“针对电子邮件枚举攻击的保护”。这会改变许多 API 的行为,包括您正在调用的 signInWithEmailAndPassword
。具体来说,API 将不再返回
'user-not-found'
错误 - 因为这些错误使得电子邮件枚举攻击成为可能。这就是为什么您的代码落入 else
块的原因。您必须禁用针对电子邮件枚举攻击的保护