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

Firebase邮箱密码创建用户异常:仅生成匿名用户且报admin-restricted-operation

Firebase邮箱密码认证无法创建正常用户问题排查

我正在为网站开发基于Firebase邮箱密码认证的账号创建功能,当前实现存在异常:必须启用匿名用户创建模式才能运行,但仅生成匿名用户,这并非预期结果。按照Firebase官方指引编写的代码却无法创建正常认证用户,执行后出现以下错误:

SignUp.jsx:20 POST https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyA4EnTbw40XnyYjHRHYjvWAxJ42STDF3cA 400 (Bad Request)
Firebase: Error (auth/admin-restricted-operation).

相关代码如下:

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

const SignUp = () => {

    const firebaseConfig = {
        apiKey: "{apiKey}",
        authDomain: "{domain}",
        projectId: "{projectID}",
        storageBucket: "{bucket}",
        messagingSenderId: "{id}",
        appId: "{id}",
        measurementId: "{id}"
    };
      
    const app = initializeApp(firebaseConfig);

    const userSignUp = ({ email, password }) => {
        const auth = getAuth(app);
        createUserWithEmailAndPassword(auth, email, password)
        .then((userCredential) => {
            const user = userCredential.user;
            console.log(user);
        })
        .catch((error) => {
            const errorCode = error.code;
            const errorMessage = error.message;
            console.log(errorMessage);
        });
    };

    function handleSignUp(e) {
        e.preventDefault();
        const form = e.target;
        const formData = new FormData(form);
        const formJson = Object.fromEntries(formData.entries());
        console.log(formJson.email + " " + formJson.password)
        userSignUp(formJson.email, formJson.password);
    }

    return(
        <>
            <h1>Sign Up</h1>
            <form method="post" onSubmit={handleSignUp}>
                <label>
                    Email: <input type="email" name="email"/>
                </label>
                <label>
                    Password: <input type="password" name="password"/>
                </label>
                <button type="submit">Submit</button>
            </form>
        </>
    );
}

export default SignUp

解决步骤:

  • 开启邮箱/密码登录方式:登录Firebase控制台,进入对应项目,打开「认证」→「登录方法」,确认「邮箱/密码」选项已启用。auth/admin-restricted-operation错误的核心原因就是该登录方式未开启,导致API请求被拒绝。
  • 修复参数传递错误:userSignUp函数定义的是接收包含email和password的对象,但调用时传了两个独立参数,导致函数内的email和password为undefined。把userSignUp(formJson.email, formJson.password)改成userSignUp(formJson)即可。
  • 避免重复初始化App:组件内部每次渲染都会重新初始化Firebase App,可能引发警告。把App初始化代码移到组件外部:
    // 组件外部初始化
    const firebaseConfig = { /* 你的配置 */ };
    const app = initializeApp(firebaseConfig);
    
    const SignUp = () => {
        // 组件逻辑
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 05:00:56