Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase getAuth() throws error getProvider of undefined but can access database

I have the following code running on a Node server.

import admin from 'firebase-admin';
import {getAuth} from 'firebase/auth';
class MyFirebase {

   constructor() {
     console.log("MyFirebase Constructor");
     this.firebaseApp = admin.initializeApp({
          credential: admin.credential.cert("PATH_TO_CERT/cert.json"),
          databaseURL: "https://DATABASE_URL",
      });
      
      console.log("App name="+firebaseApp.name);

      this.defaultAuth = getAuth(firebaseApp);
      this.database = this.firebaseApp.database();
      // database ref code here...
   }
}

and it throws the following error:

return app.container.getProvider(name); TypeError: Cannot read property 'getProvider' of undefined

If I remove "firebaseApp" from the getAuth(..) call I get this error:

No Firebase app '[DEFAULT'] has been created - call Firebase App.initializeApp() (app/no-app)

However the "console.log("App Name...")" line produces:

App name=[DEFAULT]

So clearly a DEFAULT app has been created. Additionally if I remove the "getAuth..." call the database calls pulling data from the realtime database below it work just fine, which seem to imply the authentication worked properly because I can access data from the database.

What the heck is going on?

like image 517
Scott Avatar asked Oct 26 '25 18:10

Scott


1 Answers

The new modular apis have a slightly different syntax. The following should still work if you wrap it in a class, but as long as you only do this once at the top of your express? server you shouldn't need to use a class.

Also, I'm using the require syntax but imports should work too depending on your setup.

//Import each function from the correct module.
const { initializeApp, applicationDefault } = require("firebase-admin/app");
const { getAuth } = require("firebase-admin/auth");
const { getDatabase } = require("firebase-admin/database");

const app = initializeApp({
      credential: applicationDefault(), //Don't forget to export your configuration json https://firebase.google.com/docs/admin/setup
      databaseURL: "https://DATABASE_URL",
  });

const auth = getAuth(app)
const database = getDatabase(app)

It's not super well documented but you can find hints in the Admin SDK reference: https://firebase.google.com/docs/reference/admin/node/firebase-admin.auth

One tip: In VSCode you should see the a description of each function when you hover over them, if you have the import path formatted correctly.

like image 123
Ohhh Avatar answered Oct 28 '25 08:10

Ohhh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!