【问题标题】:How do I use the local Firebase Auth emulator and not production auth to test my users?如何使用本地 Firebase 身份验证模拟器而不是生产身份验证来测试我的用户?
【发布时间】:2021-02-03 14:03:04
【问题描述】:

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

firebase init emulators

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

const handleLogin = () => 
  authentication.signInWithEmailAndPassword("emulator@test.com", "emulator");

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

【问题讨论】:

    标签: firebase-authentication firebase-tools


    【解决方案1】:

    您需要在应用的身份验证实例初始化之后立即同步调用 useEmulatoruseEmulator 将本地模拟器 URL 作为其唯一参数。

    在初始化 Firebase 身份验证实例的任何地方,您都需要以下内容:

    Firebase SDK 第 9 版(支持摇树)

    import { getAuth, connectAuthEmulator } from "firebase/auth";
    
    const auth = getAuth();
    connectAuthEmulator(auth, "http://localhost:9099");
    

    Firebase SDK 版本 8

    import firebase from "./firebase-config";
    import "firebase/auth";
    
    const authentication = firebase.auth();
    
    authentication.useEmulator("http://localhost:9099");
    
    export default authentication;
    

    【讨论】:

    • 不适用于新版本,顺便说一句 connectAuthEmulator 仅可用,没有“useEmulator”。
    • 确实 @Mycolaos 感谢您指出这一点,它在 v9 中已更改,我将更新答案
    • 我的意思是,即使在新版本中(使用connectAuthEmulator),这对我也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2021-02-24
    • 2016-10-18
    • 1970-01-01
    • 2017-03-18
    • 2021-07-31
    相关资源
    最近更新 更多