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

Firebase 9认证模拟器无法登录问题求助

解决Firebase认证模拟器登录失败问题

问题分析

你遇到的TypeError: undefined is not an object (evaluating 'credential._getIdTokenResponse')错误,核心原因有两个:

  • Auth模拟器连接协议错误(误用HTTPS而非HTTP)
  • 模拟器环境下GoogleAuthProvider.credential的使用方式不兼容

解决方案

1. 修正Auth模拟器连接协议

本地Firebase Auth模拟器默认使用HTTP协议,你配置中用HTTPS会导致连接异常,进而引发凭证处理错误。修改connectAuthEmulator的地址:

// 原错误配置
connectAuthEmulator(auth, "https://127.0.0.1:9099");
// 修改为正确配置
connectAuthEmulator(auth, "http://127.0.0.1:9099");

2. 模拟器环境下测试Google登录的正确方式

模拟器不支持直接传入自定义JWT字符串给GoogleAuthProvider.credential完成登录,推荐两种替代方案:

方案A:使用自定义Token登录

先通过Firebase CLI生成自定义Token,再调用signInWithCustomToken登录:

import { signInWithCustomToken } from "firebase/auth";

// 第一步:在终端执行CLI命令生成自定义Token
// firebase auth:login:custom --uid "abc123" --email "foo@example.com"
// 第二步:将生成的Token替换到下方代码中
signInWithCustomToken(auth, "生成的自定义Token")
  .then((userCredential) => {
    const user = userCredential.user;
    // 登录成功逻辑
  })
  .catch((error) => {
    console.error(error);
  });

方案B:创建用户后用邮箱密码登录

如果仅需测试基础认证流程,可先在模拟器中创建用户,再用邮箱密码登录:

import { createUserWithEmailAndPassword, signInWithEmailAndPassword } from "firebase/auth";

// 先创建用户(仅需执行一次)
createUserWithEmailAndPassword(auth, "foo@example.com", "test1234")
  .then(() => {
    // 用户创建成功后执行登录
    signInWithEmailAndPassword(auth, "foo@example.com", "test1234");
  })
  .catch((error) => {
    // 若用户已存在,直接执行登录
    if (error.code === 'auth/email-already-in-use') {
      signInWithEmailAndPassword(auth, "foo@example.com", "test1234");
    }
  });

3. 确保模拟器连接优先执行

确认connectAuthEmulator调用在所有Auth操作(如signInWithCredential)之前完成,保证Auth实例先绑定到模拟器环境。

验证步骤

  1. 重启Firebase模拟器服务
  2. 清除应用缓存或重启应用
  3. 重新测试登录流程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 01:25:38