Firebase Node.js SDK unauthenticated access - node.js

I am trying to access Realtime Database using the new feature introduced with 3.1.0 release: "The Node.js SDK now supports unauthenticated access. If no service account is provided, Realtime Database access will be restricted just as any unauthenticated client would be."
The SDK is updated to 3.1.0:
user#ha:~/dev/project/auth/firebase$ sudo npm install -g firebase
[sudo] password for user:
/usr/local/lib
└─┬ firebase#3.1.0
Tried with no service account:
var firebase = require('firebase');
console.log('Initialise Firebasse app');
firebase.initializeApp({
// serviceAccount: "",
databaseURL: "https://some-valid-firebase.firebaseio.com"
});
The result is:
user#ha:~/dev/project/auth/firebase$ nodejs fb_anon.js
Initialise Firebasse app
/home/user/dev/project/node_modules/firebase/auth-node/auth.js:61
throw new Error('Invalid service account provided');
^
Please help, probably I am missing something obvious here. :-(

What is happening is that your application is referring to an old sdk version that is inside your node_modules. And thats because you updated the firebase sdk with the globally flag and you are referring to a local sdk.
You should be installing it locally to your application.
First make sure you added "firebase":"3.1.0" into your package.json file and then call npm install inside the same dir.
I've just tested it with the same piece of code you added and everything went as expected.
In this question you can find some additional information and tips when using npm.

Related

Azure Error: SSL Error: CERT_UNTRUSTED when using mongoose

Have a locally running node.js app using mongoose to do crud with mlab's mongodb. No problems
I post my code to github, and then tell Azure to host my app as a webapp, which I have done many times without mongoose.
As best I can tell, Azure does an npm install on my package.json file on my behalf as it installs my app into one of their web server hosts.
If I comment out these lines in my users.js route:
//var mongoose = require('mongoose');
//var ObjectID = require('mongodb').ObjectID;
all is well, Azure runs my app.
If I do not comment them out, I get this error written in the Azure log:
npm http GET https://registry.npmjs.org/mongoose
npm ERR! Error: SSL Error: CERT_UNTRUSTED
In an effort to fix this in my package.json file, I have:
[1] forced azure to use this version of node
"engines": {
"node": "0.8.x"
},
[2] tried to force to a current version
"mongoose": "^5.3.11",
[3] tried to force negotiator to this version as GitHub was complaining about a security issue with negotiator
"negotiator": ">=0.6.1",
I would REALLY like to continue to have Azure run my web apps using GitHub, and not get into the Azure command line stuff to install my bits, so I don't have much control of the installation. There is something about Mongoose that is bad.
thanks
One thing you could try is updating to the newer certificates' definitions.
This version of config-defs.js might do the trick (usually located under /deps/npm/lib/utils).
You might also need to update that file with an additional certificate as described here
Hope it helps!
Neil Lunn fixed it, thanks. I was having problems running v10.x.x of node, and tried dropping back to V8.x.x to get around a breaking change in v10, and I screwed up the name of V8. "node": "0.8.x" does not get V8, 8.10.0 does and all is well.

firebase.database() is not a function on local node

I'm running a simple node app in my local machine and I need to connect to firebase realtime database.
I installed firebase via npm:
npm install firebase --save
Then I initialize the app:
var firebase = require("firebase");
var config = {
apiKey: "api-key",
authDomain: "my-app-database.firebaseapp.com",
databaseURL: "https://my-url.firebaseio.com",
storageBucket: "my-app-database.appspot.com",
};
firebase.initializeApp(config);
var myRef = firebase.database().ref("collection").on("value", (snap) => {
// do something with the data
});
Then I get the error that database is not a function. I check firebase.database and is undefined, also so are firebase.auth and firebase.storage.
I followed all the steps in the docs, but I don't see anything that could be causing this.
Goodness gracious... It was as simple as requiring the other packages in the file, like this:
// firebase
const firebase = require("firebase");
// get database, auth and storage
require("firebase/auth");
require("firebase/storage");
require("firebase/database");
Nowhere in the docs or reference says that. I thought about going back a version, perhaps 4.12.x, so I went to the npm page to see the previous versions and install one of those and try, when I found this:
https://www.npmjs.com/package/firebase#include-only-the-features-you-need
Just scroll down where they mention using npm packages or Typescript and you'll find the answer.
Shout out to the Firebase team, this information can't be just in the npm page and not in the docs, getting started guides or the github repo. Not a lot of people goes to the npm page of a package for information and I went there to check previous versions, so I kind of stumbled upon it.

Integrate Cloud Functions for Firebase with Braintree

I'm trying to see if it's possible to integrate Cloud Functions for Firebase with Braintree. I created a project for Cloud Functions according to the docs.
In the project directory I ran: npm install braintree.
I modified index.js for testing purposes to be the following:
const functions = require('firebase-functions');
var braintree = require("braintree");
var gateway = braintree.connect({
environment:
braintree.Environment.Sandbox,
merchantId: "useYourMerchantId",
publicKey: "useYourPublicKey",
privateKey: "useYourPrivateKey"
});
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-
functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
//gateway.clientToken.generate({}, function (err, response) {
//response.send(response.clientToken);
//});
});
When I tried to deploy this test function I got the error
Error parsing triggers: Cannot find module 'braintree'
I'm new to Firebase, Cloud Functions, and node.js and would appreciate any input on how to import Braintree to Firebase Functions project.
It looks like Cloud Functions for Firebase is not picking up the braintree module. Like most Node.js environments, Cloud Functions reads the dependencies from package.json. When you install a module with npm you can tell it to also write it to package.json by adding --save to the command line. So:
npm install braintree --save
You have the Node.js package braintree missing.
Your Firebase project has a directory called functions.
In the terminal, go to the functions directory by $ cd {your project dir}/functions
And then npm i braintree --save.
I hope that it helps you.
change current directory to functions:- cd functions
then install braintree in that folder using npm: - npm i braintree
then import braintree : - var braintree = require('braintree');
now everything should work fine.
Note:- if you have not enabled payment in firebase, it will though 'unexpectedError' in the http response.

Firebase Admin SDK initialization failed from TypeScript

I am trying to import the Firebase Admin SDK in my TypeScript (Nest.js) application.
let serviceAccount = require("../../creds.json");
console.log(serviceAccount);
const firebase = require("firebase");
firebase.initializeApp(environment.firebase);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "projectid"
});
But when I try and build the application I get the following error.
ERROR in ./node_modules/#google-cloud/firestore/src/v1beta1/firestore_client.js
Module not found: Error: Can't resolve './firestore_client_config' in '/home/jaybell/trellis-server/trellis/node_modules/#google-cloud/firestore/src/v1beta1'
# ./node_modules/#google-cloud/firestore/src/v1beta1/firestore_client.js 28:17-53
# ./node_modules/#google-cloud/firestore/src/v1beta1/index.js
# ./node_modules/#google-cloud/firestore/src/index.js
# ./src/server/main.server.ts
ERROR in ./node_modules/google-gax/lib/operations_client.js
Module not found: Error: Can't resolve './operations_client_config' in '/home/jaybell/trellis-server/trellis/node_modules/google-gax/lib'
# ./node_modules/google-gax/lib/operations_client.js 30:17-54
# ./node_modules/google-gax/index.js
# ./node_modules/#google-cloud/firestore/src/v1beta1/index.js
# ./node_modules/#google-cloud/firestore/src/index.js
# ./src/server/main.server.ts
ERROR in ./node_modules/google-gax/index.js
Module not found: Error: Can't resolve './package' in '/home/jaybell/trellis-server/trellis/node_modules/google-gax'
# ./node_modules/google-gax/index.js 65:18-38
# ./node_modules/#google-cloud/firestore/src/v1beta1/index.js
# ./node_modules/#google-cloud/firestore/src/index.js
# ./src/server/main.server.ts
I have tried to search up any connection between the admin sdk and the google-cloud firestore package but nothing has shown up. I tried to include the firestore library and initialize it as well but this error still shows up.
I included the admin sdk exactly as in the firebase docs with
import * as admin from 'firebase-admin';
after installing with
yarn add firebase-admin
I know the firebase admin sdk can communicate with firestore but unsure of why it would be throwing this error during initialization.
It might have something to do with my project including both a front end and back end component that are compiled together, Node server that serves an angular site. Could the firebase admin sdk be being compiled with the front end possibly causing this error?
Any thoughts?
Ok I think I may have solved my own problem.
The issue was was that the firebase-admin-sdk was being included in the build process and was causing it to fail.
Fix was:
install https://github.com/liady/webpack-node-externals
and add that to my webpack.config.ts file and exclude the node_modules folder.
I then had to authenticate through gcloud, instructions here:
Could not load the default credentials? (Node.js Google Compute Engine tutorial)
And now the build and serve works.
You lost add to webpack json extension for resolve
module.exports = {
...
resolve: {
extensions: ['.ts', '.js', '.json']
}
...
};

Firebase reference is empty after requiring new version

The new documentation provides a straightforward method of initialising firebase, for example, this snipped comes directly from the README.md file:
Install the Firebase npm module:
$ npm init
$ npm install --save firebase
In your code, you can access Firebase using:
var firebase = require('firebase');
var app = firebase.intializeApp({ ... });
// ...
But in my installation this is not working, as in, firebase is empty ({} when console logged).
There's another section in the documentation called Include only the features you need, which provides the following snippet:
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
var app = firebase.initializeApp({ ... });
// ...
This method works for me, so I'm wondering what could I be doing wrong in the first instance?
I'm using browserify, if that helps, but the docs state: The browser version is designed to be used with
a package bundler (e.g., Browserify,
Webpack).

Resources