如何使用本地 Firebase Auth 模拟器而不是生产身份验证来测试我的用户?

Ste*_*nos 8 firebase-tools firebase-authentication

尽管已使用以下命令在本地初始化了身份验证模拟器,但我的应用程序仍然希望使用生产 firebase-auth 实例来验证用户:

firebase init emulators
Run Code Online (Sandbox Code Playgroud)

这是我的 React 应用程序中的身份验证逻辑:

const handleLogin = () => 
  authentication.signInWithEmailAndPassword("emulator@test.com", "emulator");
Run Code Online (Sandbox Code Playgroud)

触发handleLogin后,我收到错误“auth/user-not-found”,因为 firebase 正在查询生产身份验证实例。

Ste*_*nos 7

您需要在 app\xe2\x80\x99s 身份验证实例初始化后立即同步调用useEmulator 。useEmulator将本地模拟器 URL 作为其唯一参数。

\n

无论您的 firebase auth 实例初始化,您都需要以下内容:

\n

带树摇动功能的 Firebase SDK 版本 9

\n
import { getAuth, connectAuthEmulator } from "firebase/auth";\n\nconst auth = getAuth();\nconnectAuthEmulator(auth, "http://localhost:9099");\n
Run Code Online (Sandbox Code Playgroud)\n

Firebase SDK 版本 8

\n
import firebase from "./firebase-config";\nimport "firebase/auth";\n\nconst authentication = firebase.auth();\n    \nauthentication.useEmulator("http://localhost:9099");\n    \nexport default authentication;\n
Run Code Online (Sandbox Code Playgroud)\n

  • 确实@Mycolaos 感谢您指出这一点,它在 v9 中发生了变化,我将更新答案 (2认同)