0

My code:

import admin from "firebase-admin";

const path = `./service-account-key.json`;

admin.initializeApp({
    credential: path,
});


// Get uid from user email

var uid = "";
const email = "bearcodes@outlook.com";

admin.auth().getUserByEmail(email)
    .then(function (userRecord) {
        uid = userRecord.uid;
        console.log(userRecord.uid)
    })
    .catch(function (error) {
        console.log("Error fetching user data:", error);
    });

This is the error I keep getting:

C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:44
        var _this = _super.call(this, errorInfo.message) || this;
                           ^

FirebaseAppError: Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]". The "credential" property must be an object which implements the Credential interface.
    at FirebaseAppError.FirebaseError [as constructor] (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:90:28)
    at new FirebaseAppError (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\utils\error.js:125:28)
    at new FirebaseApp (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\firebase-app.js:134:19)
    at FirebaseNamespaceInternals.initializeApp (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\firebase-namespace.js:77:19)
    at FirebaseNamespace.initializeApp (C:\Users\bearc\Downloads\flaskTesting\node_modules\firebase-admin\lib\firebase-namespace.js:387:30)
    at file:///C:/Users/bearc/Downloads/flaskTesting/applycustomclaims.js:5:7
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5) {
  errorInfo: {
    code: 'app/invalid-app-options',
    message: 'Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]". The "credential" property must be an object which implements the Credential interface.'
  },
  codePrefix: 'app'
}

Can anyone help me with what the hell is going on? I have set my package.json file with this parameter: "type": "module" so I can use the "import" statement.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Psuedo
  • 29
  • 8

2 Answers2

3

I guess you should use the admin.credential.cert(serviceAccountPathOrObject: string | admin.ServiceAccount) to wrap the credential before passing to admin.initializeApp(...)

import admin from "firebase-admin";

const path = `./service-account-key.json`;

admin.initializeApp({
    credential: admin.credential.cert(path),
});


// Get uid from user email

var uid = "";
const email = "bearcodes@outlook.com";

admin.auth().getUserByEmail(email)
    .then(function (userRecord) {
        uid = userRecord.uid;
        console.log(userRecord.uid)
    })
    .catch(function (error) {
        console.log("Error fetching user data:", error);
    });
SrHenry
  • 60
  • 3
1

Just as you see in the error message, the credential property is not supposed to be a string (which is what you're passing). It's a Credential object that has a property: getAccessToken which is a function that returns an access token object.

Here's the definition of the Credential interface:

interface Credential {

    /**
     * Returns a Google OAuth2 access token object used to authenticate with
     * Firebase services.
     *
     * This object contains the following properties:
     * * `access_token` (`string`): The actual Google OAuth2 access token.
     * * `expires_in` (`number`): The number of seconds from when the token was
     *   issued that it expires.
     *
     * @return A Google OAuth2 access token object.
     */
    getAccessToken(): Promise<admin.GoogleOAuthAccessToken>;
  }

UPDATE: According to the docs, If you're using service accounts, the value of the credential field should be the return value of the applicationDefault method. Like so:

admin.initializeApp({
  credential: admin.credential.applicationDefault()
});
JayCodist
  • 2,424
  • 2
  • 12
  • 28
  • I'm not using OAuth, I am using the `service-account-file` method. – Psuedo Aug 29 '21 at 12:11
  • That comment isn't very helpful without details. Did you follow the instructions here: https://firebase.google.com/docs/admin/setup#set-up-project-and-service-account – JayCodist Aug 29 '21 at 12:32
  • It's alright, someone else answered and hit the nail on the head :) – Psuedo Aug 29 '21 at 12:34