You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Auth signInWithEmailAndPassword无法检测错误凭证问题

问题解决:Flutter Firebase Auth登录错误提示不触发

问题原因分析

从控制台日志和代码来看,核心问题有两个:

  1. 错误码未覆盖:Firebase Auth为防止用户枚举攻击,已将「用户不存在」和「密码错误」统一返回invalid-login-credentials错误码,但你的代码仅捕获旧的user-not-found和wrong-password分支,导致错误无法触发用户可见的提示。
  2. reCAPTCHA配置异常:日志显示empty reCAPTCHA token,说明登录请求的reCAPTCHA验证未正常完成,可能是App Check未配置或密钥有误,导致错误信息传递异常。

解决方案

1. 修正异常捕获逻辑,覆盖所有错误场景

修改登录按钮的onpressed方法中的try-catch块,新增invalid-login-credentials分支处理,并添加通用异常兜底逻辑,同时用AwesomeDialog给用户显示错误提示(原代码仅打印日志,用户无法感知):

custombtn(
    textname: "Login",
    onpressed: () async {
      if (formstate.currentState!.validate()) {
        try {
          final credential = await FirebaseAuth.instance
              .signInWithEmailAndPassword(
                  email: semail.text, password: spassword.text);
          Navigator.of(context).pushReplacementNamed("home");
        } on FirebaseAuthException catch (e) {
          String errorMsg = "";
          if (e.code == 'invalid-login-credentials') {
            errorMsg = "邮箱或密码错误";
          } else if (e.code == 'user-not-found') {
            errorMsg = "未找到该用户";
          } else if (e.code == 'wrong-password') {
            errorMsg = "密码错误";
          } else {
            errorMsg = "登录失败:${e.message}";
          }
          // 弹出错误提示给用户
          AwesomeDialog(
            context: context,
            dialogType: DialogType.error,
            animType: AnimType.rightSlide,
            title: '登录失败',
            desc: errorMsg,
            btnOkOnPress: () {},
          ).show();
        } catch (e) {
          // 捕获其他非Firebase异常
          AwesomeDialog(
            context: context,
            dialogType: DialogType.error,
            animType: AnimType.rightSlide,
            title: '错误',
            desc: "发生未知错误",
            btnOkOnPress: () {},
          ).show();
        }
      } else {
        AwesomeDialog(
          context: context,
          dialogType: DialogType.error,
          animType: AnimType.rightSlide,
          title: 'خطأ',
          desc: ' لا يمكن ترك حقل اسم المستخدم أو كلمة المرور فارغ',
          btnOkOnPress: () {},
        ).show();
      }
    }),

2. 解决reCAPTCHA空token问题

  • 若启用了Firebase App Check,需确保Android端配置正确:
    1. 登录Firebase控制台,进入「App Check」模块
    2. 为Android应用添加reCAPTCHA v3验证,获取对应密钥
    3. 在android/app/build.gradle中添加依赖:
      implementation "com.google.firebase:firebase-appcheck-playintegrity:16.1.2"
      
    4. 在MainActivity中初始化App Check:
      import com.google.firebase.appcheck.FirebaseAppCheck
      import com.google.firebase.appcheck.playintegrity.PlayIntegrityAppCheckProviderFactory
      
      class MainActivity : FlutterActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              FirebaseAppCheck.getInstance().installAppCheckProviderFactory(
                  PlayIntegrityAppCheckProviderFactory.getInstance()
              )
          }
      }
      
  • 若无需App Check,可暂时关闭该功能测试(生产环境不建议关闭)。

关键说明

Firebase Auth近期调整了错误码策略,将用户不存在和密码错误合并为invalid-login-credentials,目的是避免恶意用户通过错误提示枚举有效邮箱账号,因此必须更新捕获逻辑才能正确触发错误提示。

内容的提问来源于stack exchange,提问作者انس الهريني

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 06:15:07