Firebase on Spring Boot Default App

ε祈祈猫儿з 提交于 2021-01-29 05:22:32

问题


I am creating a JHipster application (Springboot + Angular) and I need to integrate Firebase.

I've followed the instructions on firebase documentation and it works perfectly when I start the sever offline.

But when I deploy the server to AWS using jhipster aws in console, when I try to call a function that uses the firebase SDK it says Trying to login to firebase failed. Reason: FirebaseApp name [DEFAULT] already exists!

This behaviour happens only when I deploy the application because if I call it from a localhost it works perfectly.

I have this on my pom.xml

<dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>6.3.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.gms</groupId>
        <artifactId>google-services</artifactId>
        <version>3.1.1</version>
    </dependency>

and I'm initializing my app using

try{
        InputStream serviceAccount = new ByteArrayInputStream(getFirebaseJson().getBytes(StandardCharsets.UTF_8));
        //FileInputStream serviceAccount = new FileInputStream("firebaseAuth.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://seeu-soon.firebaseio.com/")
            .build();



        FirebaseApp.initializeApp(options);
        FirebaseDatabase.getInstance(FirebaseApp.getInstance()).setPersistenceEnabled(true);

    }catch(Exception e){
        log.debug("Trying to login to firebase failed. Reason: " + e.getMessage());
    }

回答1:


I fixed it.

Turns out that the FirebaseApp.initializeApp(options); function was executed once on the api when the environment was deployed to the AWS Elastic Beanstalk.

What I did was to create a class called FirebaseUtils which has a initiateFirebase() procedure, which sets the FirebaseApp.

Once I call the initiateFirebase() on each call of the API where I need access to firebase, the FirebaseApp is created with a new options configuration which is in another scope and I can access Firebase.



来源:https://stackoverflow.com/questions/51976522/firebase-on-spring-boot-default-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!