1

I'm creating an electron app as an interface for administrators. Currently using Real-time database and firestore. I'm using the firebase admin sdk with a local .json service account file to connect to the realtime database and perform all un-restricted read/write operations. Everything works as expected. However, when attempting to read/write to the Firestore database, I receive an error:

You need to pass auth instance to use gRPC-fallback client in browser. Use OAuth2Client from google-auth-library.

I did read online that other people had a similar issue and I've tried the following:

  • npm rebuild
  • install the @grcp/grcp packages
  • install the google-auth-library

Unfortunately, I still get the same response. I also get a lot of the following warning messages (each referencing a different js file), not sure if they are related:

DevTools failed to load SourceMap: Could not parse content for file:///Users/anaroca/Desktop/FCF-Admin/node_modules/@google-cloud/firestore/build/src/pool.js.map: Unexpected end of JSON input

This is how I have initiated the admin app:

var admin = require("firebase-admin");
var serviceAccount = require("./secret.json");
app = admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://project-name.firebaseio.com",
  projectId: 'project-name'
});

Any help is appreciated.

EDIT: After getting the answer from Dallas, I found this stackoverflow that was a really elegant solution to my problem.

How to use Admin SDK with limited privileges on Firestore?

sychordCoder
  • 230
  • 3
  • 14
  • Are you trying to add this to the front end electron app or on a node.JS server – Dallas Baker May 06 '21 at 01:15
  • this is a front-end electron app BUT it's for the administrators so it needs admin access to the databases to read/write/update etc. The admin SDK works for realtime database. – sychordCoder May 06 '21 at 01:30

1 Answers1

0

Firestore is new and so there are a few differences from realtime.

If you wish to implement in electron, react, or another front end framework you need to use the JavaScript SDK.

If you wish to implement Firestore on a secured server using Node.JS, c# etc you need to use the Admin SDK.

This is how I would implement using electron. Create a firebase.js file as such. Use babel to transpile if you want to use newest syntax.

import firebase from 'firebase/app';
import 'firebase/firestore';

const firebaseConfig = {
  apiKey: 'key',
  authDomain: 'domain.firebaseapp.com',
  databaseURL: 'https://domain.firebaseio.com',
  projectId: 'project1234',
  storageBucket: 'myapp.appspot.com',
  messagingSenderId: '123456789',
  appId: 'ID',
  measurementId: 'MID',
};

firebase.initializeApp(firebaseConfig);

export const firestore = firebase.firestore();

export default firebase;

Then implement as such for any place you need to access the DB

// The firebase.js file you created earlier.
import { firestore } from './firebase';

    const getPost = async (poss) => {
      const snapshot = await firestore.collection('location/of/post').get();
      const myPosts = snapshot.docs.map(collectIdsAndDocs);
      return myPosts;
    };

    const createPost = async (post) => {
      const docRef = await firestore.collection('location/of/post').add(post);
      const doc = await docRef.get();
      return doc;
    };

    createPost({ Title: 'My First Post', Content: 'My content' });
    getPost();

To secure the Database and grant admin privileges, head over to the project settings and assign users roles. You should only need to implement a service account with the Admin-SDK if you have a server that cleans up old data or users or serves another autonomous purpose.

Dallas Baker
  • 368
  • 1
  • 10
  • Hi Dallas, to my understanding though if I use the client-side SDK I will not have admin privileges? – sychordCoder May 06 '21 at 01:28
  • If you grant that user admin access then yes they can have admin access. You will need to configure security rules for the DB and assign users roles. – Dallas Baker May 06 '21 at 01:32
  • When the user authenticates from the client side (electron app) google will verify their access from firebase. You don't need to worry about that. Just make sure you have the security rules set up to make it secure and assign roles to your app users. – Dallas Baker May 06 '21 at 01:33
  • Will that then mess up the way I've setup admin access for Realtime? Would I need to just use the JS SDK for that as well and set admin privileges? – sychordCoder May 06 '21 at 01:36
  • This is if you want admin access to the DB. If you want a service account or total admin access. This needs to be implemented on a server not a client facing app. – Dallas Baker May 06 '21 at 01:36
  • I guess I don't see why they'd allow admin access for the realtime database in a front-end app for realtime but not for firestore. Oh well. – sychordCoder May 06 '21 at 01:37
  • Realtime is older and I am not sure google is putting much effort into it. Companies went with mongo for security reasons. Firestore is the new and improved DB. It requires access management in addition to security rules. Realtime doesn't support IAM. – Dallas Baker May 06 '21 at 01:43
  • https://firebase.google.com/docs/firestore/rtdb-vs-firestore#security – Dallas Baker May 06 '21 at 01:44