简体   繁体   中英

Is the firebase JS SDK package compatible with the Node.js environment of a cloud function? Why use firebase-admin?

I know that if you are on a server environment, it probably makes more sense to use the firebase-admin SDK, that will grant you admin status on a trusted environment, like the one from a cloud function.

But these two packages have always gotten me confused: firebase JS SDK and firebase-admin .

See this excerpt from the firebase JS SDK

https://www.npmjs.com/package/firebase

This SDK is intended for end-user client access from environments such as the Web , mobile Web (eg React Native, Ionic), Node.js desktop (eg Electron), or IoT devices running Node.js. If you are instead interested in using a Node.js SDK which grants you admin access from a privileged environment (like a server), you should use the Firebase Admin Node.js SDK.

For a long time, I thought that the firebase JS SDK was completely incompatible with the Node.js environment of a cloud function, and that the mere presence or a simple import * as firebase from "firebase/app"; could break my function's code. But I learned today that this is not the case. Is it?

In theory, could I use the firebase package to call firebase services from the Node.js environment of a cloud function?

Other than having admin privileges, if I'm only going to do a simple task like reading a firestore document, is there a reason why I should prefer using the firebase-admin instead of the firebase JS SDK package?

You can use firebase in a Node.js environment to access certain services. However, those service calls will be made as if they were made by an end-user. Meaning all the security rules, per-user request quotas and abuse prevention mechanisms in the Firebase backend servers come into play. If your rules only allow authenticated users to access Firestore, then you will have to authenticate as an end-user in the server prior to calling Firestore. Each invocation of the cloud function will have to do this since functions do not keep state. Running that many authentication attempts from the same IP block is likely to eventually trigger some abuse prevention safeguard, and result in errors.

As a rule of thumb, use the client SDK for client-side apps, and use the server/Admin SDK for server-side applications. There are times when you might want to bend this rule. But that should be the exception as opposed to the norm, and should be done only after careful consideration.

In theory, could I use the firebase package to call firebase services from the Node.js environment of a cloud function?

The Firestore web SDK provided by Firebase was not meant for use in nodejs environments. If it happens to work, then I would consider that a bonus. But I would not expect that at all, and I would definitely not depend on that.

is there a reason why I should prefer using the firebase-admin instead of the firebase JS SDK package?

The main reason is because firebase-admin specifically supports nodejs. That's how it was meant to work. The Admin SDK is just a wrapper around the Firestore SDK provided by Google Cloud . Once you have Firestore object, it's exactly the same stuff.

As I mentioned in your other question, the SDKs might appear similar, but they are not the same. They don't have the same features and APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM