firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firebase client sometimes tries to production server while using connectAuthEmulator

Open bunyaminmrcn opened this issue 1 year ago • 5 comments

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 [email protected]

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 }


bunyaminmrcn avatar Sep 25 '24 07:09 bunyaminmrcn

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

google-oss-bot avatar Sep 25 '24 07:09 google-oss-bot

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.

hsubox76 avatar Sep 25 '24 18:09 hsubox76

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

bunyaminmrcn avatar Sep 28 '24 15:09 bunyaminmrcn

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?

jbalidiong avatar Oct 07 '24 11:10 jbalidiong

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 avatar Oct 14 '24 01:10 google-oss-bot

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.

google-oss-bot avatar Oct 21 '24 01:10 google-oss-bot