Search code examples
node.jsfirebase-admin

Firebase Admin INVALID_APP_OPTIONS error at initializeApp()


I'm trying to connect Instagram OAuth to Firebase through Node.js back-end. I have successfully retrieved Instagram account data including access_token which I want to exchange with firebase-admin's createCustomToken on my Node.js backend. My objective here is to generate custom token so my Angular app can do signInWithCustomToken(token) into my Firebase app. There is no problem on retrieving data from Instagram as I can print my JSON object on console.

The problem is occurred when I want to exchange my access_token to Firebase Custom Token.

I have followed this guide from Firebase Admin page for Node.js and I'm facing an error message below

throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, "Invalid Firebase app options passed as the first argument to initializeApp() for the " +

Error: 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.

Here is my code on related issue.

// authService.js

var fbAdmin = require('firebase-admin');
var serviceAccount = require('./key/key.json');

function createFirebaseToken(instagramID) {

        // I copy & pasted this var from other class
        var config = {
            apiKey: "MY_FIREBASE_APIKEY",
            authDomain: "MY_APP.firebaseapp.com",
            databaseURL: "https://MY_APP.firebaseio.com",
            storageBucket: "MY_APP.appspot.com",
        };

        console.log(fbAdmin.credential.cert(serviceAccount)); // serviceAccount successfully printed on console

        // Error appears when executing this function
        fbAdmin.initializeApp({
            serviceAccount: fbAdmin.credential.cert(serviceAccount),
            databaseURL: config.databaseURL
        });

        const uid = `instagram:${instagramID}`;
      
        // Create the custom token.
        console.log(uid);
        return fbAdmin.auth().createCustomToken(uid);
    }

It appears that my Node app cannot initialize a connection to firebase-admin but I have no idea the solution as I am a beginner on these technologies. Please advice.


Solution

  • Just stumbled upon on Firebase Admin Release Notes at version 5.0.0 on May 2017 stated that serviceAccount has been removed. So instead of forcing to use serviceAccount, I use credential instead.

    fbAdmin.initializeApp({
      credential: fbAdmin.credential.cert({
          projectId: '<APP_ID>',
          clientEmail: "foo@<APP_ID>.iam.gserviceaccount.com",
          privateKey: "-----BEGIN PRIVATE KEY-----\n<MY_PRIVATE_KEY>\n-----END PRIVATE KEY-----\n"
        }),
      databaseURL: config.databaseURL
    });