5

I want to use the firebase admin SDK to give my node server free access to my database. This is the code of index.js in my functions folder:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

// Initialize app
admin.initializeApp({
  credential: admin.credential.cert("logininfo.json"),
  databaseURL: "https://thenameofmydatabase.firebaseio.com/",
  databaseAuthVariableOverride: {
    uid: "nameserver"
  }
});

In the same folder, I have my logininfo.json, which looks something like this (censored the keys for obvious reasons):

{
  "type": "service_account",
  "project_id": "...",
  "private_key_id": "...",
  "private_key": "...",
  "client_email": "...",
  "client_id": "...",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "..."
}

However, I get the error Failed to parse certificate key file: Error: ENOENT: no such file or directory when trying to deploy to firebase hosting.

How do I fix this and is there a more secure / elegant way to handle this? Can I just change the GOOGLE_APPLICATION_CREDENTIALS variable somewhere in firebase hosting?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Jan Berndt
  • 913
  • 1
  • 10
  • 22
  • Use `.env` files. Remember not to commit these files. – Sumit Ridhal Oct 17 '19 at 10:21
  • What exactly are you doing to deploy this? What is your project structure? Can you read the config file directly with your own code? How exactly can we reproduce this ourselves? – Doug Stevenson Oct 17 '19 at 10:30
  • @SumitRidhal thanks, but I still get the same error. – Jan Berndt Oct 17 '19 at 10:35
  • @DougStevenson I use firebase hosting for my website. The config file is just the private key file generated through [google service accounts](https://firebase.google.com/docs/admin/setup#initialize_the_sdk). – Jan Berndt Oct 17 '19 at 10:39
  • Firebase Hosting doesn't run node apps. It just serves static content. If you want to run node routes behind Firebase Hosting, you'll need to integrate with Cloud Functions. https://firebase.google.com/docs/hosting/functions – Doug Stevenson Oct 17 '19 at 10:46
  • @Doug I got my functions folder through `firebase init functions`. `nameserver` also shows up under my functions in the firebase dashboard. Isn't this what you mean? – Jan Berndt Oct 17 '19 at 11:18
  • When working with Cloud Functions, you don't need to provide a service account. You can use the default service account for project, which is used if you initialize the Admin SDK with no arguments. `admin.initializeApp()` - this is the most common thing to do. – Doug Stevenson Oct 17 '19 at 12:08

1 Answers1

5

It can be done by 2 different ways:

By Path:

    const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    const serviceAccount = require("path/to/logininfo.json");

    // Initialize app
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://thenameofmydatabase.firebaseio.com/",
      databaseAuthVariableOverride: {
        uid: "nameserver"
      }
    });

By Object:

const functions = require("firebase-functions");
        const admin = require("firebase-admin");

        // Initialize app
        admin.initializeApp({
          credential: admin.credential.cert({
               "type": "service_account",
               "project_id": "...",
               "private_key_id": "...",
               "private_key": "...",
               "client_email": "...",
               "client_id": "...",
               "auth_uri": "https://accounts.google.com/o/oauth2/auth",
               "token_uri": "https://oauth2.googleapis.com/token",
               "auth_provider_x509_cert_url": 
               "https://www.googleapis.com/oauth2/v1/certs",
               "client_x509_cert_url": "..."
        }),
          databaseURL: "https://thenameofmydatabase.firebaseio.com/",
          databaseAuthVariableOverride: {
            uid: "nameserver"
          }
        });
Ankit Manchanda
  • 562
  • 6
  • 21