1

I am working on an application, where it uses Next.js and Firebase.

I need to implement server-side authentication. Firebase allows connecting the server using Firebase Admin SDK.

const admin = require("firebase-admin");
const serviceAccount = require("../../../../3-firebase/service-account-key/service-account-file.json");

try {
  admin.initializeApp({
    credential: admin.credential.cert({
      client_email: process.env.FIREBASE_CLIENT_EMAIL,
      private_key: process.env.FIREBASE_PRIVATE_KEY,
      project_id: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
    }),
  });
  console.log("Initialized.");
} catch (error) {
  if (!/already exists/u.test(error.message)) {
    console.error("Firebase admin initialization error", error.stack);
  }
}

const db = admin.firestore();
export { db };

I installed the firebase-admin package using NPM and I setup firebase admin SDK using the above code by creating a separate file called "firebase-admin.js"

import { initializeApp } from "firebase/app";

const firebaseConfig = {
  apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
  authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};

const defaultApp = initializeApp(firebaseConfig);
export default defaultApp;

The above code is the default firebase application setup in a separate file called "firebase.js"

The problem I encountered is I am not able to access the admin firestore. However, I can able to access the default firestore.

What I observed is admin SDK is able to initialize using the credentials (private key). But I don't know why I can't access admin firestore. I mean when i use admin firestore the console gives error of 500 (internal server error)

Here is the error message, when I try to use admin.firestore()

{"error":{"code":"invalid-argument","name":"FirebaseError"}}

versions I am using "firebase": "^9.6.6", "firebase-admin": "^10.0.2",

Timely help is much needed

3
  • That second snippet should not be present in code that uses the Admin SDK. Does that error 500 appear when you try to use db/admin.firestore()? If so, are there any more details on the actual problem in there? Commented Mar 9, 2022 at 15:48
  • 1
    Hi Frank! I am frustrated by this firebase admin SDK. The second snippet is the default firebase as the docs suggest. Whether not to use the second snippet when admin SDK is initialized? Yeah, When I use admin.firestore(), the error appears as 500 (Internal server error) I have updated the error message in the qustion Commented Mar 10, 2022 at 9:21
  • I'm currently at this situation. I'm getting confused with the tutorials and documentation and different blogs. Commented Nov 30, 2023 at 14:17

2 Answers 2

4

You're importing the initializeApp method from the Firebase Javascript SDK. You should be importing from the Firebase Admin SDK.

import { initializeApp } from "firebase/app";

vs.

import { initializeApp } from "firebase-admin/app";
Sign up to request clarification or add additional context in comments.

Comments

-1

Using firebase-admin v10.0.2 I was able to successfully access the db adding this in my config.js file, similar to your firebase.js file.

const ServiceAccount = require('../superSecretServiceKeyFile.json');
const app = initializeApp(ServiceAccount);

const { getFirestore } = require('firebase-admin/firestore');
const db = getFirestore(app);

2 Comments

I am getting this error when I followed what you said, error - ./node_modules/@grpc/grpc-js/build/src/tls-helpers.js:20:0 Module not found: Can't resolve 'fs' Import trace for requested module: ./node_modules/@grpc/grpc-js/build/src/channel-credentials.js ./node_modules/@grpc/grpc-js/build/src/index.js ./node_modules/google-gax/build/src/index.js ./node_modules/google-gax/build/src/fallback.js ./node_modules/@google-cloud/firestore/build/src/index.js ./node_modules/firebase-admin/lib/firestore/index.js
@RoopeshSaravanan: This code is strange. initializeApp takes a credential parameter, to which you must pass cert('../superSecretServiceKeyFile.json'). See firebase.google.com/docs/firestore/quickstart#initialize -> Node.js -> Initialize on your own server. Yes, the service account parameter to cert can be a file path string directly (you don't need to require it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.