为最终用户处理 Flutter 上的 Firebase 身份验证错误

时间:2021-05-30 23:16:58

标签: firebase flutter firebase-authentication

每次出现错误时,例如在登录屏幕上:“密码无效”,我都会收到一个字符串,其中包含如下消息:

enter image description here

如何将此消息更改为用户友好的消息?我尝试使用在另一篇文章中找到的以下代码:

class Errors {
  static String show(String errMsg) {
    switch (errMsg) {
      case 'ERROR_EMAIL_ALREADY_IN_USE':
        return "This e-mail address is already in use, please use a different e-mail address.";

      case 'ERROR_INVALID_EMAIL':
        return "The email address is badly formatted.";

      case 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL':
        return "The e-mail address in your Facebook account has been registered in the system before. Please login by trying other methods with this e-mail address.";

      case 'ERROR_WRONG_PASSWORD':
        return "E-mail address or password is incorrect.";

      default:
        return "An error has occurred";
    }
  }
}

但它总是在屏幕上显示默认错误。我尝试在案例中使用“firebase_auth/wrong-password”或“wrong-password”,但没有奏效。有人有更好的解决方案吗?我是新来的!

2 个答案:

答案 0 :(得分:0)

尝试在此处阅读此文档:

https://firebase.flutter.dev/docs/auth/usage

正如您在文档中看到的,字母是小写字母并用“-”分隔。

答案 1 :(得分:0)

I searched a bit on the post from ZOthix and i did find the solution.

I post it here for anyone who needs it:
class Errors {
  static String show(String errMsg) {
    switch (errMsg) {
      case "email-already-in-use":
        return "Email already used. Go to login page.";
        break;
      case "wrong-password":
        return "Wrong email/password combination.";
        break;
      case "user-not-found":
        return "No user found with this email.";
        break;
      case "user-disabled":
        return "User disabled.";
        break;
      case "ERROR_TOO_MANY_REQUESTS":
      case "operation-not-allowed":
        return "Too many requests to log into this account.";
        break;
      case "ERROR_OPERATION_NOT_ALLOWED":
      case "operation-not-allowed":
        return "Server error, please try again later.";
        break;
      case "invalid-email":
        return "Email address is invalid.";
        break;
      default:
        return "Login failed. Please try again.";
        break;
    }
  }
}