How to initialize firebase admin using modular Firebase v9?
Asked Answered
L

2

5

I'm using firebase admin to interact with Firestore and auth from a custom backend (next.js api routes).

Firebase recently announced they're going with modular tree-shaking packages.

How can I refactor below code to utilize tree-shaking firebase module?

import * as firebaseAdmin from "firebase-admin";

if (!firebaseAdmin.apps.length) {
  const adminCredentials = {
    credential: firebaseAdmin.credential.cert({
      projectId: env.fbProjIdPublic,
      clientEmail: env.fbClientEmail,
      privateKey: JSON.parse(env.fbPvtKey),
    }),
    databaseURL: env.fbDbUrlPublic,
  };

  firebaseAdmin.initializeApp(adminCredentials);
}

export default firebaseAdmin;

So far I couldn't find replacement for firebaseAdmin.credential.cert.

Lycian answered 2/10, 2021 at 19:46 Comment(0)
F
6

The documentation you linked is for the Web/JavaScript SDK, not for the Node.js Admin SDKs.

A modular Admin SDK for Node.js is under development, but not generally available yet. You can find the latest information and how to join the alpha program here.

You can also track its progress on the Github repo, like on this recent feature request, and this request for feedback from the engineers working on it.

Fungi answered 2/10, 2021 at 23:34 Comment(1)
is it advisable to use modular version as v10 is now released github.com/firebase/firebase-admin-node/releases/tag/v10.0.0 ?Lycian
A
2

I've tried and this works.

Env variables not being strings was my problem on local

import { initializeApp, App, AppOptions, getApps, getApp, cert } from "firebase-admin/app";


const clientEmail = process.env.FIREBASE_CLIENT_EMAIL;
const privateKey = process.env.FIREBASE_PRIVATE_KEY;
const projectId = process.env.FIREBASE_PROJECT_ID;

const firebaseAdminConfig: AppOptions = {
  credential: cert({
    clientEmail,
    privateKey,
    projectId,
  }),
};
Arabinose answered 2/11, 2021 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.