1

Right now I am attempting to set up the firebase admin for a firebase project I am trying to create. The project is an admin site that is supposed to upload data to an app. The point is to create a site that trusted people can access so those people don't have to log into the project owner's email to enter this data. The site is also supposed to help manage those users. I need to be able to use the firebase admin functions to create and edit users.

Therefore, I need to add the Admin SDK to my project. It says to add it to a Node.js project which is what I have. I followed all the instructions for the basic setup. I then tried to initialize the admin functions from a function in my html page. I realize I could do it via an external file, but as a newer user of many of these functions and having been through a large headache trying to setup these external files before, I felt I would set up the thing in the page for now to get all the functions working then move it all to an external file later. However, when I attempt to test my code, I get the require is not defined error. I did some research to find a solution and came upon the module.config.js approach. I attempted to set it up the way they did, but the question was related to just running the Firebase SDK. This is the first time I have heard of Webpack and don't have a massive background in web design. You could say its a hobby ATM.

Basically, rn I need help setting up this config file so that I can run

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

I copied and pasted the base webpack.config.js file from https://webpack.js.org/configuration/

and placed it in my directory which looks like this: The main folder: https://i.stack.imgur.com/KIp3l.png

and the files in dist which is basically my entire project: https://i.stack.imgur.com/Aq2Pj.png https://i.stack.imgur.com/IP87C.png

Can someone explain what needs to be done to run the admin initialization so I can run the user editing commands on my project? I know what I need to do once this is set up, but I don't know what I need to do to set it up so once I get this done, I will be pretty much set.

  • This is a node app right? or are you trying to use Firebase Admin in a web app? – Dharmaraj Jul 23 '21 at 02:42
  • Under the classifications of the firebase projects, its a web app and that is what it is categorized as when I deploy it to firebase. However, I used node.js and npm to create the project initially. So I believe it is a node.js project being uploaded to a web app. It does have the node modules, etc. Is this a problem? To the best of my understanding, I am trying to set up the Firebase Admin SDK on a project to be run in Firebase as a web app, but has all the files for a node.js app and I believe to be a node.js project. – Josh Solders Jul 23 '21 at 02:54

2 Answers2

0

The documentation also mentions it,

If you are interested in using the Node.js SDK as a client for end-user access (for example, in a Node.js desktop or IoT application), as opposed to admin access from a privileged environment (like a server), you should instead follow the instructions for setting up the client JavaScript SDK.

Even if you have node modules installed but the environment is not NodeJS, it's a browser. Also Admin SDK has privileged access to your Firebase projects and should not be used on client side as anyone may use it and get full access. How are you going to restrict the web app to specific users anyway? Firebase hosting has no such option for that.

You should consider using Firebase Callable Functions and use Admin SDK in them. Then just call your functions from your web app as required. You can authenticate users in the cloud function and do all verification so it's much safer.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • I just want to check because the documentation for the callable functions isn't quite as clear as I would prefer for my current exposure level. I should be able to use Functions.https.onCall to run admin.auth().updateUser(uid,{...}); and functions to write to and read from Firestone and other database? If that is the case, which I believe it is, could you give an example of setting up a updateUser call. If I am going to be using this instead, I would like to see a good example of executing that specific command since they didn't discuss it in the linked document. – Josh Solders Jul 23 '21 at 04:26
  • @JoshSolders checkout [this answer](https://stackoverflow.com/questions/68138652/do-you-need-firebase-admin-sdk-when-creating-admin-web/68139189#68139189) and [this](https://stackoverflow.com/questions/68327600/firebase-custom-claims-without-cloud-functions/68327673#68327673). I've explained setting up callable function and calling them from frontend. – Dharmaraj Jul 23 '21 at 04:31
  • TBH, I looked and I still don't really understand. I might be a bit too unfamiliar with this or something. Whatever the case, it's not really clicking. If would be really helpful if you just dropped an example for the updateUser command. Just showing how to set it up with a command I am familiar with would make this a lot simpler for me. I do understand that I might seem a bit dimwitted considering I can't figure this out when you have given me so many examples, so I thank you for your time and patience. However, could you please do this for me so I can fully understand what how to useit – Josh Solders Jul 23 '21 at 05:23
0

The exact way to initialize the firebase app can vary, depending on which version of firebase-admin you're using. For my current project, my package.json says that I am using 11.4.1. Here is how I was able to successfully import & initialize:

import * as admin from 'firebase-admin'; // change your import

const firebaseCreds = require("../path/to/your-firebase-adminsdk.json")

admin.initializeApp({
  credential: admin.credential.cert(firebaseCreds),
  serviceAccount: firebaseCreds // add serviceAccount
});
Zernach
  • 95
  • 1
  • 1
  • 11