Reputation: 37
I hope you can help me I am making an API with Node.js (stateless) connected to firebase the main objective is to create users and work with collections (Add, edit, consult) information I am using the following firebase module
const firebase = require('firebase');
firebase.initializeApp(firebaseConfig);
When I try to insert data, as such there is no user logged in, and the methods to insert information or delete or modify, I get back user permission errors.
code example:
const db = firebase.firestore();
let botRef = db.collection('bot').doc(bot.botId);
botRef.get().then(function(doc) {
if (doc.exists) {
botRef.update({ color: bot.color, botname: bot.botname, welcomemessage: bot.welcomemessage });
What should I do? or should i use the admin module of firebase? since as such in this API there is no login to firebase (since it is a stateless API) thanks for your help
Upvotes: 0
Views: 187
Reputation: 50830
Replace the modules like this:
import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
By the way, don't forget to install firebase-admin
before deploying your program. Run this command to install it:
$ npm install firebase-admin --save
in your terminal.
This will grant admin permissions to your cloud functions which means no restrictions on reading or writing the data.
Even if you have any firebase realtime database or cloud firestore rules, it will override them.
Upvotes: 0
Reputation: 317362
You should use the Firebase Admin SDK for this. It just wraps the Google Cloud Firestore SDK for node, so it has the same API as that.
You will need to initialize the SDK with a service account. Then, your code will have full read and write access to everything in the database, bypassing any security rules you have set up for actual users.
Upvotes: 1