How to add Firebase auth onCreate listener in Admin Sdk - firebase

I want to run a listener in Firebase Admin SDK that runs whenever a new account is created. It is possible to do this in cloud functions as shown here: https://firebase.google.com/docs/functions/auth-events#trigger_a_function_on_user_creation. Can the same be done in Firebase Admin SDK?

Nope. There is no onCreate listener in the Auth SDKs.
Since creating a user from the authentication SDK happens completely under control of your code, you're expect to handle any downstream actions in your own code once the user creation completes.
For example, in the documentation on creating a user with the Admin SDK for Node.js, you can put any code that needs to run after the user has been created in the then() callback.

Related

Flutter Notifications with Firebase Realtime Database

I have a Flutter app integrated with a Firebase Realtime Database.
I want a notification (actually an alarm kind of thing if possible), when an item (lets call it alarm) in the database is set to "true". I always see firebase_messaging plugin for notifications, but I am not sure if I'm supposed to use this plugin despite that my app doesn't have anything to do with messaging.
I am totally new to both Flutter and Firebase, can you tell me how to listen to the database even if the app is not running?
By the way, I am currently building the app for only Android, but I want to build it for IOS too in the future.
Thanks.
You can use/write firebase cloud fucntion. Using firebase cloud function you can watch any document/field and try to write trigger logic like if a field is set to true then this cloud function will throw an notification via firebase messaging.
https://firebase.flutter.dev/docs/functions/overview/
When the user is not actively using the app there is no reliable way to continue to listen to changes in the Firebase Realtime Database. To notify the user of changes to the database in that situation, you'll need to listen for those change on an environment that is always on, and then send a message to the user through Firebase Cloud Messaging.
One environment that is always in is Cloud Functions, which is also part of Firebase, and allows you to run small snippets of JavaScript code on Google's servers in response to things that happen in your Firebase project. The documentation of Cloud Functions for Firebase as example of how to notify the user when something interesting happens in the database:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

Admin Notifications in Firebase Firestore

Is there any easy way admin to get notification whenever a new document added to fire store, we are completed the shopping app with android studio and fire base. user successfully placing orders but admin has to go fire base console and to check if any new order.
please help me to get notified by email or any other method. fire base functions may be the solution but i am completely new to functions.
There is nothing built into Firestore to send an email when a new document is added.
But you can use the Firestore integration with Cloud Functions to trigger such an email. In fact, that is precisely what the Firebase trigger email extension does. It's a click-to-install extension, so I recommend giving that a try and see if it fits your needs.

Firebase Mailchimp Extension - Update Subscriber Name

Is there a way to update the subscriber when the user updates their profile with their first/last name? Could that be done with a cloud function if not through the extension?
Thanks in advance
[can this be done] when the user updates their profile with their first/last name
There is currently no Cloud Functions trigger for Firebase Authentication profile updates. This means that there is no way to do this directly with Cloud Functions, and hence also not in the Auth Mailchimp sync Extension, which is based on Cloud Functions functionality.
If you build this functionality yourself (you can use the existing extension source code for inspiration), you can trigger an update of the Mailchimp audience directly from your client-side code when you also call the Firebase Authentication API to update the profile.

How to notify a Firebase Admin SDK user using a cloud function?

I am a student developing an app and I have some back-end python code that utilizes the Firebase admin SDK. The app with provide an interface to an proprietary algorithm that cannot be moved into the a cloud function and so must stay within the back-end server. When a user makes a request to the algorithm, they will do so by uploading a document to Firestore with information the back-end needs to process their request.
Once a user uploads a document, an onCreate() cloud function is triggered, the goal of this function is to simply notify the back-end that there is a pending request, so that it can process it and send back to the user.
This is where I am struggling, I haven't been able to deduce a way to trigger action on the back-end from within the cloud function. I am hoping to find a way to accomplish this through Firebase without the need to implement additional libraries etc.
A way to generalize my issue would be:
How would you notify an Firebase Admin SDK user through a Cloud Function?
FCM is used for sending messages and notifications to mobile clients. It doesn't work for sending messages to backend components.
If you want to notify some backend component, you typically use an HTTP endpoint or pubsub messaging.
Firebase Functions SDK already uses the Firebase Admin SDK under the hood. So if you set up a Cloud Functions trigger in Node.js, you can access Admin SDK from the Function itself. For instance, taking a Cloud Storage trigger as an example:
const admin = require('firebase-admin');
admin.initializeApp();
exports.fn = functions.storage.object().onFinalize((object) => {
// Call Admin SDK APIs
});
Similar integrations should be possible with Python as well: https://medium.com/#hiranya911/firebase-using-the-python-admin-sdk-on-google-cloud-functions-590f50226286

Firebase Cloud Function Authentication Trigger timing

Does anyone knows what the timing of the Firebase Cloud Functions onCreate Authentication Trigger is?
Is it like:
- User registers using SDK
- Firebase creates user in and for Firebase Authentication
- Firebase SDK sends login successful event
- Function onCreate is invoked
or like:
- User registers using SDK
- Firebase creates user in and for Firebase Authentication
- Firebase onCreate is invoked
- Firebase SDK sends login successful event
Or in other words:
Can I be sure that after a successful user registration a Firestore User Document was already created by a short Firebase Cloud Function script?
So can I be sure that when the SDK send the authentication successful callback that the Firestore user document was created?
You are given no guarantees about the timing of the delivery of events to your Cloud Function code. Of course, the system is going to try to deliver as fast as possible. But the fact of the matter is that there can be unpredictable delays at every stage through the processing of that event, so you shouldn't depend on any sort of specific timing. This is especially true when also dealing with other systems that work asynchronously as well, including Firestore (it sounds like you're implying that your function creates a document to read back later in the app).
The good news is that you can listen to the document that you expect to be created for the user, and receive it whenever it's ready.
As Doug has said, sadly there are no guarantees onCreate will be evoked in time, but here is my thought solution.
Because the onCreate hook can only access the auth user information, you will have to have a post-creation function. So I see two options:
it's worth having the post-creation function handle the case if the db record isn't created by the onCreate hook
OR
Don't even use the onCreate hook since it's almost redundant.

Resources