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

Firebase Auth JS SDK短信多因素认证收不到验证码问题排查

Firebase Auth短信多因素认证收不到验证码排查

问题场景

  • 使用Firebase Auth JS SDK,仅短信多因素认证功能异常,其余功能正常
  • 当前为Spark计费套餐,已关联账单
  • Auth项目已启用身份平台,且开启了邮箱/密码、电话两种认证方式
  • 真实以色列手机号已添加至测试号码列表
  • 注册代码执行流程表面正常:用户创建成功,隐形reCAPTCHA验证通过(标记1输出正常),verifyPhoneNumber执行完成并触发回调(标记2输出正常),但始终收不到短信验证码
  • 使用FirebaseUI Demo测试同一手机号可正常接收验证码

相关代码

createUserWithEmailAndPassword(window.accountManager.auth, this.email, this.password).then((response) => {
    //some actions
}).then(async (response) => {
    const recaptchaVerifier = new RecaptchaVerifier("recaptcha-container", {
        "size": "invisible",
        "callback": function(response) {
            console.log(response); // (1)
        }
    }, window.accountManager.auth);
    const mfaSession = await multiFactor(window.accountManager.user).getSession();
    const phoneInfoOptions = {
        phoneNumber: '+972.......', //my real Israeli phone number 
        session: mfaSession
    };
    const phoneAuthProvider = new PhoneAuthProvider(window.accountManager.auth);
    phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier)
        .then(function (verificationId) {
            console.log(verificationId); // (2)
        });
});

排查方向与解决方法

  • 用户实例正确性检查:确认window.accountManager.user是否是刚创建的用户实例。第一个then的response返回的是用户创建凭证,需要把response.user赋值给window.accountManager.user,否则multiFactor(window.accountManager.user)可能引用旧实例或未初始化对象,导致MFA会话无效,表面回调触发但实际短信未发送。
  • reCAPTCHA上下文验证:检查页面中id="recaptcha-container"元素是否在调用RecaptchaVerifier时已渲染完成。隐形reCAPTCHA依赖用户交互触发有效验证,FirebaseUI会自动处理交互逻辑,自定义代码若缺少触发逻辑,可能出现验证回调显示通过但实际未完成有效校验的情况。
  • 手机号格式确认:确保以色列手机号格式完全正确(比如原号是05x-xxxxxx需转为+9725xxxxxxx,去掉开头的0),不要有多余空格或符号,细微格式差异可能导致短信路由失败。
  • 添加错误捕获逻辑:当前代码缺少catch块,无法捕获getSession和verifyPhoneNumber的隐藏错误(比如会话过期、权限不足)。给这两个方法添加错误打印:
    const mfaSession = await multiFactor(window.accountManager.user).getSession().catch(err => {
        console.error('获取MFA会话错误:', err);
        throw err;
    });
    
    phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier)
        .then(function (verificationId) {
            console.log(verificationId); // (2)
        })
        .catch(err => {
            console.error('验证码请求错误:', err);
        });
    
    错误日志会直接定位问题根源。
  • 项目配置一致性检查:确认自定义代码使用的Firebase配置与FirebaseUI Demo完全一致,同时在Firebase控制台查看短信发送记录,确认配额是否充足(Spark套餐有免费短信额度,若用尽需关联账单升级)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 09:27:49