0

Firebase config class :

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "firebase")
public class FirebaseConfig {



@PostConstruct
public void init() {

    try {
        FirebaseApp.getInstance();
    } catch (IllegalStateException e) {
        try {
            FileInputStream inputStream = new FileInputStream("json-file-path");
            try {
                FirebaseOptions options = new FirebaseOptions.Builder()
                        .setCredentials(GoogleCredentials.fromStream(inputStream)).build();

                FirebaseApp.initializeApp(options);
            } catch (IOException ioE) {
                ioE.printStackTrace();
            }
        } catch (NullPointerException nullE) {
            nullE.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

}

I also tried to use a class like this:

public class FirebaseCredentialsHelper {

  public String type= "";
  public String project_id= "";
  public String private_key_id= "";
  public String private_key= "";
  public String client_email= "";
  public String client_id= "";
  public String auth_uri= "";
  public String token_uri= "";
  public String auth_provider_x509_cert_url= "";
  public String client_x509_cert_url= "";

}

Unfortunately when I build the app the error " FirebaseApp with name [DEFAULT] doesn't exist." comes out. In local works fine. ps: It's set up flexible

dune98
  • 167
  • 1
  • 11
  • hey @dune98, does [this solve your issue](https://stackoverflow.com/questions/37342403/firebaseapp-with-name-default-doesnt-exist)? if not, can you provide me with an explanation of how your app is structured, is it [something like this](https://cloud.google.com/solutions/mobile/mobile-firebase-app-engine-flexible)? can you also provide me with the full exception logs? – Methkal Khalawi Apr 07 '20 at 09:44
  • 1
    Locally works, only if deployed on remote server not work – dune98 Apr 11 '20 at 11:03
  • hey @dune98 , I understand that your app doesn't work when you deploy it on App Engine Flex. Can you edit your post with the logs related to this issue from your Stackdriver logging? [check here on how to view logging](https://cloud.google.com/logging/docs/view/overview). I would like that you post your app.yaml file and tell me how are you deploying your app. – Methkal Khalawi Apr 13 '20 at 07:29

1 Answers1

0

This may not apply to Android. I'll update this when I get there. if I get there

To rule off some possibilities:

("For Web Apps specifically") As per their documentation, and contrary to many stack overflow responses, the runtimeOnly dependency "com.google.gms:google-services:4.3.10" is not needed as of version 8.x.x...

One thing I noticed, is after specifying a name for the app, and trying to FirebaseApp.getInstance(name), it was still not recognizing that the app was instantiated.

Another caveat I noticed, if you improperly supply the GoogleCredentials to the application using .getApplicationDefault(), the Spring app will still build itself. But trying to fetch the services will break because the Firebase service itself wasn't built. It apparently has its own built-in exception handling even if the service internally goes kaplooe... It's a good safe check to sout your cred's to make sure they're instantiated.

The Fix: @DependsOn https://www.baeldung.com/spring-depends-on

NOTE: How you load your credentials doesn't matter, as long as it finds them

Write a bean that initializes Firebase and yeets it with IoC. Provide the "dataSource" name for the Service Annotation, and a name attribute for the Bean itself.

@Service("dataSource")
class GCP {

    @Bean(name = ["Firebase"])
    fun initializeFirebase() {

        val opts = FirebaseOptions.builder()
            .setCredentials(GoogleCredentials.fromStream(
                this::class.java.getResourceAsStream(System.getenv("GCP"))
            ))
            .build()

        FirebaseApp.initializeApp(opts)
    }
}

Now where ever you use FirestoreClient.getFirestore() (Or a diff service), make sure you take advantage of the @DependsOn Annotation to wait and make sure that Firebase was instantiated.

@Service
@DependsOn(value = ["Firebase"])
class UserService(
    private val firestore: Firestore = FirestoreClient.getFirestore()
)
Kwuasimoto
  • 71
  • 1
  • 4