I am creating a serverless function for firestore. I am using the firebase-admin SDK which requires this code to initialise
var admin = require("firebase-admin");
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
Now the serviceAccountKey.json should not be on the repo… and for the localhost it’s on my machine and it should work fine… but how do I add it to netlify without adding it to github?
One way I would assume is to use the environment variables and that’s possible… just that there’s some issues with the keys having a ‘\n’ character.
1) However I’d just like to confirm if such a requirement of adding an extra file is possible or not… 2)) If it’s not possible, is the environment variable the only option or is there some other way?
You can add an encrypted version of the file and decrypt it using a password stored as an environment variable. But if you wish to avoid that, using the values as environment variables directly is the only (secure) option.
Hi Hrishikesh
I am currently using it as an environment variable and I get this error message
Service account object must contain a string “private_key” property.
But I am using the same key that I am using on my local host… I’ve seen this post on netlify that mentioned the issue with the ‘\n’ character in the private key… Using Firebase Admin SDK with Lambda Function
But I am not sure how to use their solution… there’s a mention of using JSON.parse() but I could not really understand it…
Please do advise… my site name is ‘world-eyewear-store’, if that helps…
I’ve updated the variables a bit based on what I read online… so I read at some places that we should use quotes for the private keys… and then at other places it mentioned that we shouldn’t use opening and closing quotes for strings… so for now, I am not using opening and closing quotes on my string environment variables on netlify…
and now the error has changed to this… here’s the stack trace
[
"Error: Failed to parse private key: Error: Invalid PEM formatted message.",
" at new ServiceAccount (/var/task/functions/registerVote.js:30783:17)",
" at new ServiceAccountCredential (/var/task/functions/registerVote.js:30720:136)",
" at cert2 (/var/task/functions/registerVote.js:31663:54)",
" at Object.<anonymous> (/var/task/functions/registerVote.js:126488:15)",
" at Module._compile (node:internal/modules/cjs/loader:1105:14)",
" at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)",
" at Module.load (node:internal/modules/cjs/loader:981:32)",
" at Function.Module._load (node:internal/modules/cjs/loader:822:12)",
" at Module.require (node:internal/modules/cjs/loader:1005:19)",
" at require (node:internal/modules/cjs/helpers:102:18)"
]
I managed to solve it based on a few suggestions that were already tested and tried out.
For those who stumble upon the same issue as mine and try out the various solutions offered, there are 2 which seem to be working well for others… the JSON.parse() method and the replace() method that seems to get rid of the ‘\n’ character in the private key…
the JSON.parse() didn’t seem to work and quite frankly I don’t know how it gets rid of the ‘\n’… so if someone does know how this works please do share it with me in the responses below… I’d like to know more about this…