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

Firebase登录遇凭证错误异常,SnackBar提示未触发求解决

问题描述

Initial task failed for action RecaptchaAction(action=signInWithPassword)with exception - The supplied auth credential is incorrect, malformed, or has expired.

无论输入正确或错误的邮箱/密码,都会触发上述错误。用户已针对FirebaseAuthException做了分支处理,尝试通过自定义SnackBar展示提示并打印异常信息,但均无效果。

用户提供的相关代码如下:

登录逻辑代码

sigin(String signinEmail, String signinPassword,
    BuildContext context) async {
  try {
    await FirebaseAuth.instance.signInWithEmailAndPassword(
        email: signinEmail, password: signinPassword);

    if (!context.mounted) return;
    Navigator.push(context,
        MaterialPageRoute(builder: (context) => const HomePage()));
  } on FirebaseAuthException catch (e) {
    if (e.code == 'invalid-email') {
      showSnackbarMessage(context, 'Invalid Email');
        print('Firebase Authentication Exception: ${e.code}/////////////');

    } else if (e.code == 'user-not-found') {
      showSnackbarMessage(context, 'User not found for this Email');
        print('Firebase Authentication Exception: ${e.code}/////////////');

    } else if (e.code == 'wrong-password') {
      showSnackbarMessage(context, 'Wrong Password');
        print('Firebase Authentication Exception: ${e.code}/////////////');

    }
  } catch (e) {
    showSnackbarMessage(context, 'Unexpected error during sign-in: $e');
      print('Firebase Authentication Exception: $e/////////////');

  }
}

自定义SnackBar代码

void showSnackbarMessage(BuildContext context, String message) {
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: Text(message),
      backgroundColor: Colors.red,
      duration: const Duration(seconds: 3),
      behavior: SnackBarBehavior.floating,
       margin: const EdgeInsets.only(bottom: 600,right: 20,left: 20),
    ),
  );
}
解决方案

1. 补全FirebaseAuthException分支处理

当前代码仅覆盖了3种错误码,而触发的Recaptcha相关错误(如invalid-recaptcha-token、recaptcha-check-failed)未被捕获,导致异常直接进入通用catch块。修改异常捕获逻辑,覆盖更多场景:

on FirebaseAuthException catch (e) {
  if (!context.mounted) return;
  
  if (e.code == 'invalid-email') {
    showSnackbarMessage(context, '无效邮箱格式');
    print('Firebase Auth Error: ${e.code}');
  } else if (e.code == 'user-not-found') {
    showSnackbarMessage(context, '该邮箱未注册');
    print('Firebase Auth Error: ${e.code}');
  } else if (e.code == 'wrong-password') {
    showSnackbarMessage(context, '密码错误');
    print('Firebase Auth Error: ${e.code}');
  } else if (e.code == 'invalid-recaptcha-token' || e.code == 'recaptcha-check-failed') {
    showSnackbarMessage(context, '人机验证失败,请重试');
    print('Firebase Auth Error: ${e.code}');
  } else {
    showSnackbarMessage(context, '验证错误:${e.message ?? '未知错误'}');
    print('Firebase Auth Error: ${e.code} - ${e.message}');
  }
}

2. 确保上下文有效性

在通用catch块中先判断上下文是否可用,避免因上下文销毁导致SnackBar无法展示:

catch (e) {
  if (context.mounted) {
    showSnackbarMessage(context, '登录失败:$e');
  }
  print('Unexpected Error: $e');
}

3. 排查Firebase Recaptcha配置

  • 确认Firebase控制台中,对应Android/iOS应用的SHA-1/SHA-256指纹已正确配置,这是Recaptcha验证的必要条件。
  • 检查App Check服务是否开启,且已正确配置Recaptcha作为验证提供商。
  • 测试阶段可临时关闭App Check(仅用于排查,生产环境不建议),验证是否为配置问题导致的全局错误。

4. 调整SnackBar显示位置

当前margin: const EdgeInsets.only(bottom: 600,...)会让SnackBar被推出屏幕外,修改为合理数值:

margin: const EdgeInsets.only(bottom: 20, right: 20, left: 20),

内容的提问来源于stack exchange,提问作者user8

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 16:15:25