Firebase database functions onWrite/Create/Delete user UID [duplicate] - firebase

This question already has answers here:
Getting the user id from a database trigger in Cloud Functions for Firebase?
(2 answers)
Closed 5 years ago.
Is is possible to get the user UID of the user who wrote/updated/deleted data from the database?

When you are using Firebase authentication you can get the uid of the authenticated user using the following lines of code:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String uid = firebaseUser.getUid();
}
On the other hand, you need to know that Firebase does not store meta-data or other informations regarding write operation. To achieve this, you need to create your own mechanism.
In your case, when a user creates a record, store his uid on a location from where you can query for that particular uid and use it for what you want.
In case you are trying to determine this within Cloud Functions i recomand you take a look at Firebase Cloud Functions and at jacobawenger's answer from this post

Related

Firebase Auth REST API - Adding more fields to Sign up [duplicate]

This question already has answers here:
How to save extra info during user signup process using firebase authentication
(4 answers)
Adding extra information to registration user Firebase
(1 answer)
Closed 2 years ago.
I'm trying to add more fields to accounts:signUp e.i
fields needed: firstname surname phone...
https://firebase.google.com/docs/reference/rest/auth#section-create-email-password
so far I can pass the email and password and a user will be created, how do I add more fields? and store that information in a users collection in cloudfirestore?
Firebase Auth does not have a facility for storing arbitrary user data along with an account. You're supposed to use a database to store that, and link the record you store there to the user ID that Firebase Auth assigns to the user.

How can I use multiple order by in real time database flutter to order data by specific field? [duplicate]

This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 2 years ago.
I am trying to short "realtime database" result according to "date" in flutter. But I cant use multiple orderByChild('child_name'). It throw an error. My code is
final String path = 'jsondata';
final _dbRef = FirebaseDatabase.instance.reference();
_dbRef.child(path)
.orderByChild('trade_code').equalTo('GP')
.once()
.then((DataSnapshot snapshot) {
snapshot.value.forEach((key, value) {
print(value);
});
});
The result is
Result
Now I want to sort the data by Date.
How can I do that?
It's not possible to use multiple oder at the same time in Firebase Realtime Database. Please check the following doc:
Firebase Realtime Database Query
To achieve that type of complex query I prefer that you should migrate from the Realtime Database to Firebase Cloud Firestore. Check the following resource:
Simple and Complex Dynamic Query in Could Firestore
Your Firestore instance would be:
FirebaseFirestore.instance
.collection('products')
.where('trade_code', isEqualTo: 'GP')
.orderBy('date')

Get userId inside firebase storage trigger [duplicate]

This question already has answers here:
Upload Image to Firebase Storage with the current users UID
(1 answer)
Get uid in firebase function onFinalize for firebase storage
(1 answer)
GET UID from firebase cloud functions
(1 answer)
Closed 2 years ago.
How to get uid of the authenticated user from the storage trigger below:
exports.storageSizeCounter = functions.storage.object().onFinalize(async (object, context) => {...});
auth/uid is available during security rule evaluation but it seems like it's not passed to the trigger (like in context.auth).
Any way to get the uid inside the storage trigger?

firebase firestore check if document with property and value exists [duplicate]

This question already has answers here:
Cloud Firestore: Enforcing Unique User Names
(8 answers)
Closed 3 years ago.
I am currently trying to verify before a user creates data in the database if the data exists. Even unter an other Document id.
Like this: User creates data, firestore rules gets all documents in collection and checks if the property name is the same as from the user provided. If yes, return unauthorized access.
At this point I have:
function checkIfCatExists(){
return get(/databases/$(database)/documents/category/$(documents)).data.name != null;
}
But this does not work. Do you guys have an idea? I could create a function for that but I want to do as much as possible with rules.
There is no way to search a collection for a specific value in security rules, as that operation wouldn't scale.
If you want to ensure unique user names, you'll have to create a collection where you use the user names as the key. You can then use the exists function in your security rules, to check if the name already exists in the collection.
Also see:
Cloud Firestore: Enforcing Unique User Names
Firestore security rule to check if character username already exists
Firestore security rules - can I query for a document with specific fields?
I want to write a rule that will don't allow add same document second time

firebase realtime db checking a record for exist [duplicate]

This question already has answers here:
Test if a data exist in Firebase
(3 answers)
Closed 4 years ago.
I am using angularfire2 wth my ionic app and have sign in with facebook enabled.
When first time a user logs in using the fb then i naturally dnt have any record of it in my firebase DB. I want to create a user node using the facebook returned uid as the node.
The way i can retrieve it is like below. I can check by seeing if it has child objects etc. But is that the way recommended to do this kind of checks? I also don't see how to add subscribe or then to it as i would assume this should be async operation for me to wait until it confims me that user does not exist yet. Please advise
const userRef = this.db.object("/users/" + uid)
Simply building a reference to some location in the database (as shown in the code above) says nothing about whether or not data exists at that location. The location needs to be queried in order to figure out of data exists there.
The sample code get from firebase web API document.
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
updateStarCount(postElement, snapshot.val());
});

Resources