Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ankushdharkar/771377736a83397d1e68b3de46ff230c to your computer and use it in GitHub Desktop.
Save ankushdharkar/771377736a83397d1e68b3de46ff230c to your computer and use it in GitHub Desktop.
How to import the service account keys config into your code, especially from environment variables
const admin = require('firebase-admin');
const firestoreConfig = process.env.FIRESTORE_CONFIG; // **String** of firestore service account keys https://cloud.google.com/iam/docs/creating-managing-service-account-keys
// `{
// "type": "service_account",
// "project_id": "project-id",
// "private_key_id": "key-id",
// "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key\n-----END PRIVATE KEY-----\n",
// "client_email": "service-account-email",
// "client_id": "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/service-account-email"
// }`
const credentialsObject = JSON.parse(firestoreConfig);
admin.initializeApp({
credential: admin.credential.cert(credentialsObject)
});
const db = admin.firestore();
module.exports = db;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment