Home > Software design >  Are you supposed to pass the firebase app to getAuth() or leave the arguments as blank?
Are you supposed to pass the firebase app to getAuth() or leave the arguments as blank?

Time:06-11

The firebase docs show them using the app instance inside the getAuth() call, in one portion of the docs

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

Then in the very next section on the same page, they go ahead and use getAuth() without the app instance, in order to sign in a user with email.

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

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

What is the correct way to use it?

CodePudding user response:

The getAuth() function without parameters will use the default initialized instance of Firebase. If you have multiple instances of Firebase i.e. multiple projects, then you must pass the app instance to specify which project's auth that auth instance must use.

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
  • Related