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

FirebaseAuthException无email字段求助:account-exists-with-different-credential

Firebase Auth中Facebook登录抛出"account-exists-with-different-credential"异常但email字段为null的问题

我有一个使用Firebase Auth的Flutter应用,支持Facebook、Gmail和邮箱/密码登录,项目未启用Email enumeration protection。

遇到以下问题场景:

  • 用户通过邮箱/密码创建账户后退出;
  • 用户尝试通过Facebook登录;
  • 抛出code为"account-exists-with-different-credential"的FirebaseAuthException,但email字段为null。此前该异常的email字段从未为空,缺少email将无法按文档调用fetchSignInMethodsForEmail方法。

signInWithFacebook函数

Future<UserCredential> signInWithFacebook() async {
    final loginResult = await facebookAuth.login();

    switch (loginResult.status) {
      case LoginStatus.success:
        final accessToken = loginResult.accessToken!;

        // 此处抛出FirebaseAuthException,因为同一邮箱已存在邮箱/密码账户
        return auth.signInWithCredential(
          FacebookAuthProvider.credential(accessToken.token),
        );

      case LoginStatus.cancelled:
        throw FirebaseAuthException(
          code: 'ERROR_FACEBOOK_LOGIN_FAILED',
          message: loginResult.message,
        );
      case LoginStatus.failed:
      case LoginStatus.operationInProgress:
        throw UnimplementedError('Login Status is not implemented yet.');
    }
  }

异常捕获代码

try {
  await signInWithFacebook();
} on FirebaseAuthException catch (error) {
  if (error.code == 'account-exists-with-different-credential') {
    // error.email当前为null,此前并非如此
    await FirebaseAuth.fetchSignInMethodsForEmail(error.email!);
  }
}

pubspec.yaml配置

environment:
  sdk: '>=3.0.0 <4.0.0'
  flutter: 3.13.9

dependencies:
  android_id: ^0.3.6
  audio_waveforms: ^1.0.4
  badges: ^3.1.2
  cached_network_image: ^3.3.0
  carousel_slider: ^4.2.1
  cloud_firestore: ^4.9.3
  cloud_functions: ^4.4.2
  connectivity_plus: ^5.0.1
  cupertino_icons: ^1.0.6
  device_info_plus: ^9.1.0
  dotted_border: ^2.1.0
  firebase_analytics: ^10.5.1
  firebase_app_check: ^0.2.0+1
  firebase_auth: ^4.10.1
  firebase_core: ^2.17.0
  firebase_crashlytics: ^3.3.7
  firebase_database: ^10.2.7
  firebase_messaging: ^14.6.9
  firebase_remote_config: ^4.2.7
  firebase_storage: ^11.2.8
  firebase_ui_firestore: ^1.5.12
  flash: ^3.0.5+1
  flutter:
    sdk: flutter
  flutter_dotenv: ^5.1.0
  flutter_facebook_auth: ^6.0.2
  flutter_hooks: ^0.20.3
  flutter_image_compress: ^2.0.4
  flutter_launcher_icons: ^0.13.1
  flutter_local_notifications: ^16.1.0
  flutter_localizations:
    sdk: flutter
  flutter_slidable: ^3.0.0
  flutter_svg: ^2.0.7
  geocoding: ^2.1.1
  geolocator: ^10.1.0
  go_router: ^11.1.4
  google_sign_in: ^6.1.5
  hooks_riverpod: ^2.4.3
  http: ^1.1.0
  image_cropper: ^5.0.0
  image_picker: ^0.8.9
  intl: ^0.18.0
  just_audio: ^0.9.35
  mask_text_input_formatter: ^2.5.0
  material_design_icons_flutter: ^6.0.7296
  open_mail_app: ^0.4.5
  package_info_plus: ^4.2.0
  path: ^1.8.3
  path_provider: ^2.1.1
  permission_handler: ^11.0.1
  photo_view: ^0.14.0
  purchases_flutter: ^4.13.0
  rate_my_app: ^2.0.0
  rxdart: ^0.27.7
  scrollable_positioned_list: ^0.3.8
  share_plus: ^7.2.0
  shared_preferences: ^2.2.2
  shimmer: ^3.0.0
  showcaseview: ^2.0.3
  stack_trace: ^1.11.0
  store_redirect: ^2.0.1
  swipe_to: ^1.0.2
  timeago: ^3.5.0
  url_launcher: ^6.1.14
  uuid: ^3.0.7
  webview_flutter: ^4.4.1

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_web_plugins:
    sdk: flutter
  mocktail: ^1.0.1
  very_good_analysis: ^5.1.0

请问有人知道这是怎么回事吗?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 14:04:55