简体   繁体   中英

Firebase Emulator - Spring boot - FirebaseApp with name [DEFAULT] doesn't exist

I have a spring boot app using a Firebase (Emulator) running in a docker container. This is my configuration

   GoogleCredentials credentials = GoogleCredentials
   .create(new AccessToken(
   "mock-token", Date.from(LocalDateTime.now()
   .plusYears(1).atZone(ZoneId.systemDefault()).toInstant())));

    FirebaseOptions options = FirebaseOptions.builder()
            .setCredentials(credentials)
            .setProjectId("my_project")
            .setDatabaseUrl("http://localhost:9000/")
            .build();
    FirebaseApp.initializeApp(options, "my_project");
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();  

running FirebaseAuth.getInstance() , i got this error: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. Available app names: my_project

What i am doing wrong?.

connect to firebase emulator from spring boot app.

The getInstance call documentation reads:

Returns an instance of this class corresponding to the default FirebaseApp instance.

the initializeApp must not have set a default FirebaseApp instance.

What you can do it take the app returned by initializeApp and pass it in, like: getInstance(myApp) .

I'd also use the useEmulator function rather than setting the DB URL: https://firebase.google.com/docs/emulator-suite/connect_auth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM