Reputation: 4895
I am hosting a node.js
function on google firebase, and I would like to create and update user accounts on the server side. My service account json
file looks like:
{
"apiKey" : ""
"authDomain" : ""
"databaseURL" : ""
"projectId" : ""
"storageBucket" : ""
"messagingSenderId": ""
}
I removed the fields for reasons. When I load the credential and create an admin
instance via:
import * as admin from 'firebase-admin' ;
import * as functions from 'firebase-functions';
const accountKeyPath = path.join(__dirname, '../credentials/serviceAccountKey.json')
const accountKey = require(accountKeyPath);
const firebaseAdmin = admin.initializeApp( accountKey );
The firebaseAdmin
is sufficient to crud
a database. However it cannot create new users unless I declare it with:
const firebaseAdmin = admin.initializeApp({
credential: admin.credential.cert(accountKey)
, databaseURL: "..."
});
Or so it appears. When I run this I get error:
throw new
error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
^
Error: Certificate object must contain a string "private_key" property.
This is inexplicable to me because I do not know how to get a service account json
with private_key
. Am I suppose to create one each time I run the server?
Upvotes: 1
Views: 1958
Reputation: 23042
Your JSON is completely incorrect for the firebase-admin
SDK. Please see the docs on how to retrieve the service account JSON file: https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app
It should have the following format:
{
"type": "service_account",
"project_id": "...",
"private_key_id": "...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"
}
Edit:
I noticed you're importing firebase-functions
in which case you don't need to service account JSON at all. Simply call:
admin.initializeApp(functions.config().firebase);
See https://firebase.google.com/docs/admin/setup#initialize_the_sdk
Upvotes: 2