簡體   English   中英

配置 Firebase 身份驗證以發送 INVALID_CREDENTIALS 而不是 EMAIL_NOT_FOUND 或 INVALID_PASSWORD

[英]Configure Firebase Authentication to send INVALID_CREDENTIALS instead of EMAIL_NOT_FOUND or INVALID_PASSWORD

我正在使用免費的 Firebase 計划來測試我的 React 應用程序,並且我現在正在使用電子郵件和密碼提供程序進行身份驗證。

我在我的客戶端直接使用 Firebase。

我設法處理了 UI 中的身份驗證錯誤,並在用戶的電子郵件或密碼錯誤時向用戶顯示一條消息,如Invalid credentials ,但我仍然在網絡選項卡中收到來自 firebase 的EMAIL_NOT_FOUNDINVALID_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);
            }
          }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM