我正在使用免费的 Firebase 计划来测试我的 React 应用程序,并且我现在正在使用电子邮件和密码提供商进行身份验证。
我直接在客户端中使用 Firebase。
我设法处理 UI 中的身份验证错误,并在用户的电子邮件或密码错误时向用户显示类似
Invalid credentials
的消息,但我仍然在网络选项卡中收到来自 firebase 的 EMAIL_NOT_FOUND
或 INVALID_PASSWORD
响应。
这就是我处理 UI 的方式
try {
setFirebaseError("");
await signInWithEmailAndPassword(auth, values.email, values.password);
} catch (err) {
if (["auth/wrong-password", "auth/user-not-found"].includes(err.code)) setFirebaseError("Invalid credentials");
else setFirebaseError(err.message);
}
但答案是要么
{
"error": {
"code": 400,
"message": "EMAIL_NOT_FOUND",
"errors": [
{
"message": "EMAIL_NOT_FOUND",
"domain": "global",
"reason": "invalid"
}
]
}
}
或
{
"error": {
"code": 400,
"message": "INVALID_PASSWORD",
"errors": [
{
"message": "INVALID_PASSWORD",
"domain": "global",
"reason": "invalid"
}
]
}
}
我不想要这样。
有没有办法配置 Firebase Authenticaton 来发送自定义错误响应?
是的,你可以做到
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: "your_given_email",
password: "your_given_password!"
);
} on FirebaseAuthException catch (e) {
if (e.code == 'network-request-failed') {
showErrorDialog(context, 'No Internet Connection');
//devtools.log('No Internet Connection');
} else if (e.code == "wrong-password") {
return showErrorDialog(
context, 'Please Enter correct password');
//devtools.log('Please Enter correct password');
//print('Please Enter correct password');
} else if (e.code == 'user-not-found') {
showErrorDialog(context, 'Email not found');
// print('Email not found');
} else if (e.code == 'too-many-requests') {
return showErrorDialog(
context, 'Too many attempts please try later');
//print('Too many attempts please try later');
} else if (e.code == 'unknwon') {
showErrorDialog(
context, 'Email and password field are required');
//print('Email and password field are required');
} else if (e.code == 'unknown') {
showErrorDialog(
context, 'Email and Password Fields are required');
//print(e.code);
} else {
print(e.code);
}
}
对于遇到此问题的任何人,请确保您启用了电子邮件枚举保护。请参阅此处了解更多详细信息: https://cloud.google.com/identity-platform/docs/admin/email-enumeration-protection#enable