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

Flutter Firebase OTP验证过程中出现意外跳转问题求助

Flutter Firebase手机号OTP验证跳转到外部网站问题

问题详情

在Flutter应用中使用Firebase进行手机号OTP验证时,启动验证流程后应用会跳转到外部网站显示“Verifying not a robot”提示,之后才返回APP的OTP输入界面。预期是应用内部完成整个OTP验证流程,无需跳转外部网站。

已尝试的解决步骤:

  • 检查Firebase项目设置中是否存在意外的重定向URL;
  • 排查代码中是否存在意外的导航操作;
  • 在不同设备上测试以排除设备特定问题。

相关代码:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:get/get.dart';


class AuthenticationRepository extends GetxController
{
  static AuthenticationRepository get instance => Get.find();
  final _auth = FirebaseAuth.instance;
  late final Rx<User?> firebaseUser;
  var verificationId = ' '.obs;

  @override
  void onReady()
  {
    firebaseUser = _auth.currentUser as Rx<User?>;
    firebaseUser.bindStream(_auth.userChanges());
    ever(firebaseUser, _setInitialScreen);
  }

  _setInitialScreen(User? user)
  {
    //user == null ? Get.offAll(() => const WelcomeScreen()) : Get.offAll(() => const Dashboard());
  }

  Future<void> phoneAuthentication(String phoneNo) async {
    await _auth.verifyPhoneNumber(
        phoneNumber: phoneNo,
        verificationCompleted: (credentials) async{
          await _auth.signInWithCredential(credentials);
        },
        verificationFailed: (e){
          if (e.code == 'invalid-phone-number'){
            Get.snackbar('Error', 'The provided phone number is not valid');
          }
          else{
            Get.snackbar('Error', 'Something went wrong try again !');
          }
        },
        codeSent: (verificationId, resendToken){
          this.verificationId.value = verificationId;
        },
        codeAutoRetrievalTimeout: (verificationId){
          this.verificationId.value = verificationId;
        }
    );
  }

  Future<bool> verifyOTP(String otp) async{
    var credentials = await _auth.signInWithCredential(PhoneAuthProvider.credential(verificationId: this.verificationId.value, smsCode: otp));
    return credentials.user != null ? true : false;
  }


}

解决方案

这种跳转外部网站的情况,本质是Firebase的reCAPTCHA验证被触发,通常因为当前环境无法通过设备级安全验证,或者应用未完成Firebase的合法性校验。以下是针对性解决步骤:

1. 添加应用签名指纹到Firebase项目

Firebase需要验证应用合法性,未添加签名指纹会强制触发reCAPTCHA跳转:

  • 打开Firebase控制台,进入目标项目 → 项目设置 → 应用板块;
  • 找到对应的Flutter应用,添加Android的SHA-1和SHA-256指纹(iOS无需此操作);
  • 添加完成后,下载最新的google-services.json(Android)或GoogleService-Info.plist(iOS),替换项目中对应文件。

2. 使用真实物理设备测试

模拟器/仿真器通常无法通过Firebase的设备级验证,会触发reCAPTCHA跳转。改用真实手机测试,多数情况下可避免跳转。

3. 配置Firebase App Check(可选但推荐)

App Check能帮助Firebase验证请求来自合法应用,降低reCAPTCHA触发概率:

  • 在Firebase控制台启用App Check;
  • 为Android配置SafetyNet,为iOS配置DeviceCheck;
  • 在Flutter项目中集成firebase_app_check包,完成初始化配置。

4. 升级Firebase依赖包

老旧的firebase_auth等包可能存在兼容性问题,导致reCAPTCHA异常跳转。在pubspec.yaml中升级到最新版本:

firebase_auth: ^latest_version
firebase_core: ^latest_version

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:12:49