您是否应该将 firebase 应用程序传递给 getAuth() 或将参数保留为空白?

say*_*ode 4 javascript firebase firebase-authentication

firebase 文档在文档的一部分中向他们展示了在 getAuth() 调用中使用应用程序实例

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
  // ...
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app); //I'm talking about this line
Run Code Online (Sandbox Code Playgroud)

然后在同一页面的下一部分中,他们继续使用 getAuth() 而不使用应用程序实例,以便使用电子邮件登录用户。

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

const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    const user = userCredential.user;
  })
  .catch((error) => {
    console.log(error)
  });
Run Code Online (Sandbox Code Playgroud)

正确的使用方法是什么?

Dha*_*raj 13

不带参数的函数getAuth()将使用 Firebase 默认初始化的实例。如果您有多个 Firebase 实例(即多个项目),则必须传递应用程序实例来指定身份验证实例必须使用哪个项目的身份验证。

const app1 = initializeApp(project1Config);
const app2 = initializeApp(project2Config, "app2");

const auth1 = getAuth(); // or getAuth(app1), uses app1 as it's default, 

const auth2 = getAuth(app2); // uses app2
Run Code Online (Sandbox Code Playgroud)