EhsanR
EhsanR

Reputation: 467

FirebaseApp with name [DEFAULT] doesn't exist ; Firebase Token Verification

in order to verify a Firebase token sent from a client app i have the following code:

Task<FirebaseToken> authTask = FirebaseAuth.getInstance().verifyIdToken("very long string token").addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Object tr) { }
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception excptn) { }
}).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Task task) { }
});
try {
Tasks.await(authTask);
} catch(ExecutionException | InterruptedException e ){
//handle error
}
FirebaseToken decodedToken = authTask.getResult();
String uid = decodedToken.getUid();

When deploy it through Maven/google backend api I get the following error:

java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist

How can I resolve it?

Upvotes: 1

Views: 5494

Answers (1)

Nipper
Nipper

Reputation: 2380

  1. You have to set up a Service Account at your Google Cloud Console. Read the Docs.

  2. Put the service account key file (.json) in your /resources/META-INF/ directory.

  3. Make sure you have included your resource files inside the appengine-web.xml

<!-- Include resource files -->
<resource-files>
    <include path="/**.json" />
    <include path="/**.xml" />
    <include path="/**.properties" />
</resource-files>
  1. Pass over the FirebaseOptions with the generated service_account_key.json file to initialize FirebaseApp. You can do this inside a Warmup Servlet.
public void init(ServletConfig config) throws ServletException {

    InputStream loadedServiceAccount = config
        .getServletContext()
        .getResourceAsStream("/WEB-INF/service_account_key.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
        .setServiceAccount(loadedServiceAccount)
        .setDatabaseUrl("URL_FIREBASE_DATABASE").build();

    FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
    FirebaseAuth.getInstance(firebaseApp);

}
  1. Then you can go on using FirebaseAuth.getInstance() inside your code.

When your AppEngine Instance runs a beta Version (maybe 1.9.44) it will give you a noMethodFoundError. Then you can go on with this Question.

Upvotes: 2

Related Questions