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

Firebase Web端手机号登录报错auth/operation-not-allowed求助

解决Firebase手机号认证auth/operation-not-allowed错误

问题描述

基于React实现Firebase手机号认证功能时,仅能使用Firebase测试设置中配置的号码完成登录,使用其他号码时抛出以下错误:

FirebaseError: Firebase: Error (auth/operation-not-allowed).
    at createErrorInternal (assert.ts:136:1)
    at _fail (assert.ts:65:1)
    at _performFetchWithErrorHandling (index.ts:177:1)
    at async _verifyPhoneNumber (phone.ts:239:1)
    at async signInWithPhoneNumber (phone.ts:106:1)

相关代码如下:

App.js核心逻辑

function onCaptchVerify() {
    if (!window.recaptchaVerifier) {
      window.recaptchaVerifier = new RecaptchaVerifier(
        "recaptcha-container",
        {
          size: "visible",
          callback: (response) => {
            onSignup();
          },
          "expired-callback": () => {},
        },
        auth
      );
    }
  }

  function onSignup() {
    setLoading(true);
    onCaptchVerify();

    const appVerifier = window.recaptchaVerifier;

    const formatPh = "+" + ph;

    signInWithPhoneNumber(auth, formatPh, appVerifier)
      .then((confirmationResult) => {
        window.confirmationResult = confirmationResult;
        setLoading(false);
        setShowOTP(true);
        toast.success("OTP sended successfully!");
      })
      .catch((error) => {
        console.log(error);
        setLoading(false);
      });
  }

  function onOTPVerify() {
    setLoading(true);
    window.confirmationResult
      .confirm(otp)
      .then(async (res) => {
        console.log(res);
        setUser(res.user);
        setLoading(false);
      })
      .catch((err) => {
        console.log(err);
        setLoading(false);
      });
  }

setup.js初始化代码

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

const app = initializeApp(firebaseConfig);

const auth = getAuth(app);

export {auth};

已尝试在授权域名(含localhost)、不同设备及多个Firebase项目账号中测试,均出现相同问题。


解决方案

1. 启用Firebase电话号码登录方法

登录Firebase控制台,进入对应项目:

  • 打开Authentication模块,切换到登录方法标签页
  • 找到电话号码选项,确认其状态为启用。若为禁用状态,点击编辑按钮启用该登录方式。

2. 退出测试模式(允许真实号码登录)

在电话号码登录方法的设置页面:

  • 找到测试电话号码区域,若当前处于测试模式(仅允许添加的测试号码登录),点击退出测试模式
  • 若需正式发送短信,需配置短信服务商(如Twilio、Plivo)或使用Firebase内置短信服务(需完成项目验证并确保账户有可用额度)

3. 验证手机号格式正确性

确保代码中拼接的手机号格式符合国际标准:

  • 确认ph变量仅包含数字,无多余字符(如空格、括号)
  • 最终格式应为+[国家代码][手机号],例如+8613800138000

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:40:53