firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.86k stars 891 forks source link

Firebase client sometimes tries to production server while using connectAuthEmulator #8519

Closed bunyaminmrcn closed 3 weeks ago

bunyaminmrcn commented 1 month ago

Operating System

Kali Linux

Environment (if applicable)

Chrome 129, Firefox 130

Firebase SDK Version

10.13.2

Firebase SDK Product(s)

Auth

Project Tooling

next project with next-auth@4.24.7

Detailed Problem Description

I set it up local emulator for firebase auth & firestore & storage & functions. But issue happen while authorizing

Steps and code to reproduce issue

I run initEmulator function in useEffect hook after login. It asks to credentials to production server (https://identitytoolkit.googleapis.com/v1/accounts:lookup)

import { initializeApp, getApp } from 'firebase/app';
import { connectAuthEmulator, getAuth } from 'firebase/auth';

const firebaseConfig = {
  apiKey: "test",
  authDomain: 'http://127.0.0.1:9099',
  databaseURL: "http://127.0.0.1:8080",
  projectId: "test",
};

const firebaseApp = initializeApp(firebaseConfig);

let initialized = false;
const initEmulator = () => {
  if (!initialized) {
    try {

      connectAuthEmulator(firebaseAuth, 'http://127.0.0.1:9099')
      console.log("Connected to Emulator")
      initialized = true;
    } catch(err) {
      console.log("Error occured")
    }

  }
}

const firebaseAuth = getAuth(firebaseApp)
export { firebaseApp, firebaseAuth, initEmulator }
google-oss-bot commented 1 month ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

hsubox76 commented 1 month ago

I'm not sure I am understanding correctly - you're running initEmulator after login? Isn't it too late? Doesn't the login operation itself call the backend, if no emulator has been initialized? Or do you mean you're running it after initializing auth, not login?

Regardless, useEffect has some weird timing and usually happens after initial page load. I don't think I'd feel safe putting anything in there that I want to run before the rest of my code. I'd probably start the emulator outside of any component. It's not a UI operation.

bunyaminmrcn commented 1 month ago

Put initEmulator to directly in component doesn't effect . I am using Next . Its run on the backend first and window is not defined. It gives an error

jbalidiong commented 1 month ago

Hi @bunyaminmrcn, I tried replicating the issue but wasn't able to reproduce the same behavior. I created this repo to try and replicate the setup you have. In case I missed anything or made a mistake with the setup, just let me know. Alternatively, any chance you could provide an MCVE to help us replicate the issue?

google-oss-bot commented 1 month ago

Hey @bunyaminmrcn. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 3 weeks ago

Since there haven't been any recent updates here, I am going to close this issue.

@bunyaminmrcn if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.