How to use the firebase-admin.apps function in firebase-admin

To help you get started, we’ve selected a few firebase-admin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pubpub / pubpub / tools / 5to6 / frankenbook / migrate-frankenbook.js View on Github external
/* eslint-disable no-console */
import fs from 'fs';
import uuidv4 from 'uuid/v4';
import firebaseAdmin from 'firebase-admin';
import { Discussion, Pub, Collection, CollectionPub, Version } from '../v5/models';
import { nestDiscussionsToThreads } from '../../../client/containers/Pub/PubDocument/PubDiscussions/discussionUtils';

const serviceAccount = JSON.parse(
	Buffer.from(process.env.V5_FIREBASE_SERVICE_ACCOUNT_BASE64, 'base64').toString(),
);

const firebaseApp =
	firebaseAdmin.apps.length > 0
		? firebaseAdmin.apps[0]
		: firebaseAdmin.initializeApp(
				{
					credential: firebaseAdmin.credential.cert(serviceAccount),
					databaseURL: process.env.V5_FIREBASE_URL,
				},
				'firebase-pub-new',
		  );
const database = firebaseApp.database();

const workingDirectory = process.env.MIGRATION_WORKING_DIRECTORY;
const frankenbookFile = fs.readFileSync(`${workingDirectory}/frankenbook-firebase.json`);
const frankenbookVersionFile = fs.readFileSync(`${workingDirectory}/frankenbook-version.json`);
const frankJson = JSON.parse(frankenbookFile);
const frankVersionJson = JSON.parse(frankenbookVersionFile);
github decentralized-identity / element / packages / element-api / src / services / firebase.js View on Github external
const fs = require('fs');
const path = require('path');
const firebaseAdmin = require('firebase-admin');
// Required for side-effects
require('firebase/firestore');

let serviceAccountKey = false;
const serviceAccountPath = path.resolve(
  __dirname,
  '../../element-did-firebase-adminsdk.json',
);

if (fs.existsSync(serviceAccountPath)) {
  serviceAccountKey = JSON.parse(fs.readFileSync(serviceAccountPath));
}
if (!firebaseAdmin.apps.length) {
  firebaseAdmin.initializeApp({
    credential: serviceAccountKey
      ? firebaseAdmin.credential.cert(serviceAccountKey)
      : firebaseAdmin.credential.applicationDefault(),
    databaseURL: 'https://element-did.firebaseio.com',
  });
}

const firestore = firebaseAdmin.firestore();

module.exports = {
  firestore,
};
github erxes / erxes-api / src / data / utils.ts View on Github external
export const sendMobileNotification = async ({
  receivers,
  title,
  body,
  customerId,
}: {
  receivers: string[];
  customerId?: string;
  title: string;
  body: string;
}): Promise => {
  if (!admin.apps.length) {
    return;
  }

  const transporter = admin.messaging();
  const tokens: string[] = [];

  if (receivers) {
    tokens.push(...(await Users.find({ _id: { $in: receivers } }).distinct('deviceTokens')));
  }

  if (customerId) {
    tokens.push(...(await Customers.findOne({ _id: customerId }).distinct('deviceTokens')));
  }

  if (tokens.length > 0) {
    // send notification
github Canner / gqlify / packages / gqlify-firestore / src / index.ts View on Github external
constructor(option: FirestoreOption) {
    this.db = isEmpty(admin.apps)
      ? admin.initializeApp(option.config).firestore()
      : admin.app().firestore();
    this.collection = option.collection;
    // path exists
    if (option.path && option.path.indexOf('/') > 0) {
      this.path = option.path;
    } else if (option.path) {
      this.path = option.path.startsWith('/')
        ? `${defaultMapCollection}${option.path}`
        : `${defaultMapCollection}/${option.path}`;
    }
  }
github erxes / erxes-api / src / data / utils.ts View on Github external
export const sendMobileNotification = async ({
  receivers,
  title,
  body,
  customerId,
  conversationId,
}: {
  receivers: string[];
  customerId?: string;
  title: string;
  body: string;
  conversationId: string;
}): Promise => {
  if (!admin.apps.length) {
    return;
  }

  const transporter = admin.messaging();
  const tokens: string[] = [];

  if (receivers) {
    tokens.push(...(await Users.find({ _id: { $in: receivers } }).distinct('deviceTokens')));
  }

  if (customerId) {
    tokens.push(...(await Customers.findOne({ _id: customerId }).distinct('deviceTokens')));
  }

  if (tokens.length > 0) {
    // send notification
github firebase / firebase-functions / spec / apps.spec.ts View on Github external
afterEach(() => {
    _.forEach(firebase.apps, (app) => {
      app.delete();
    });
  });
github JoeRoddy / firestation / src / db / FirebaseDb.js View on Github external
const killFirebaseApps = function() {
  admin.apps.forEach(app => {
    app.delete();
  });
};
github syousif94 / frugalmaps / functions / firebase.js View on Github external
const admin = require("firebase-admin");

const serviceAccount = require("./firebase.json");

if (!admin.apps.length) {
  admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://frugalmaps.firebaseio.com"
  });
}

const db = admin.firestore();

module.exports = {
  db
};
github JoeRoddy / firestation / src / db / FirebaseDb.js View on Github external
const _getAppIfAlreadyExists = url => {
  let apps = admin.apps;
  for (let i = 0; i < apps.length; i++) {
    if (apps[i].name === url) {
      return apps[i];
    }
  }
  return null;
};
github taessina / gatsby-source-firestore / gatsby-node.js View on Github external
exports.sourceNodes = async (
  { boundActionCreators },
  { types, credential, appConfig }
) => {
  try {
    if (firebase.apps || !firebase.apps.length) {
      const cfg = appConfig ? appConfig : {credential: firebase.credential.cert(credential)}
      firebase.initializeApp(cfg);
    }
  } catch (e) {
    report.warn(
      'Could not initialize Firebase. Please check `credential` property in gatsby-config.js'
    );
    report.warn(e);
    return;
  }
  const db = firebase.firestore();
  db.settings({
    timestampsInSnapshots: true,
  });

  const { createNode } = boundActionCreators;