Created
January 15, 2021 18:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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