Skip to content

Commit

Permalink
BO39
Browse files Browse the repository at this point in the history
  • Loading branch information
Camillo Bucciarelli committed May 2, 2024
1 parent 9b1e42d commit 506930e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
27 changes: 19 additions & 8 deletions functions/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { ServiceAccount, cert, initializeApp } from "firebase-admin/app";
import {
cert,
getApps,
initializeApp,
ServiceAccount,
} from "firebase-admin/app";
import * as dotenv from "dotenv";

// load environment variables
dotenv.config({ path: "../.env" });
const serviceAccount = JSON.parse(
process.env.PUBLIC_FIREBASE_SERVICE_ACCOUNT || "{}",
);

// initialize firebase app
export const initialize = () =>
initializeApp({
credential: cert(serviceAccount as ServiceAccount),
});
const initializeFirebaseApp = () => {
if (getApps().length === 0) {
initializeApp({
credential: cert(serviceAccount as ServiceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
}
};

export const functionsRegion = process.env.PUBLIC_FIREBASE_FUNCTIONS_REGION || "";
const projectId = serviceAccount.project_id;

const functionsRegion = process.env.PUBLIC_FIREBASE_FUNCTIONS_REGION || "";

export { initializeFirebaseApp, projectId, functionsRegion };
4 changes: 2 additions & 2 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as httpApi from "./api/http/api";
import * as authOnCreate from "./api/auth/on-create";
import { initialize } from "./config";
import { initializeFirebaseApp } from "./config";

initialize();
initializeFirebaseApp();

exports.http_api = httpApi.default;
exports.onCreateUser = authOnCreate.default;
4 changes: 2 additions & 2 deletions functions/test/api/auth/on-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai";
import { getAuth } from "firebase-admin/auth";
import { DocumentSnapshot, getFirestore } from "firebase-admin/firestore";
import { before, describe, test } from "mocha";
import {initialize} from "../../../src/config";
import { initializeFirebaseApp } from "../../../src/config";

describe("onCreate", () => {
const collection = "users";
Expand All @@ -13,7 +13,7 @@ describe("onCreate", () => {
};

before(async () => {
initialize();
initializeFirebaseApp();
});

test("Create user and check if it saved in users collection", async () => {
Expand Down

0 comments on commit 506930e

Please sign in to comment.