Get userId inside firebase storage trigger [duplicate] - firebase

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?

Related

How to use parameters from "where" in Firestore security rules? [duplicate]

This question already has answers here:
Firebase Firestore security rule: Only allow collection group query if filtering by a specific field
(2 answers)
Firestore Security Rules: Passing a request parameter in a get() call?
(1 answer)
Can I send value with request to Firestore rules
(2 answers)
Firebase Firestore - security rule based on "where" query parameters
(1 answer)
Closed last year.
I have the following Firestore query which can be invoked by a non-Firebase user (the sessionId is just a session id - not a Firebase user id):
const q = query(collection(firestore, "apples"), where("sessionId", "==", sessionId));
I am thinking that I need a Firestore rule to prevent access to the whole apples collection. I just want the user to access the documents where the sessionId field matches the sessionId of the requester.
I tried (which does not work):
match /apples/{appleID} {
allow read: if request.resource.data.sessionId == resource.data.sessionId;
}
But if I have understood the reference correctly - request.resource is only available on write requests.
How can I solve this issue?

Firebase query to perform posts search Cloud Firestore [duplicate]

This question already has answers here:
Firestore group collection query using Flutter
(2 answers)
Fetch all the posts of all the users from Cloud Firestore
(1 answer)
How to retrieve all data from a subcollection of all document
(1 answer)
Closed 1 year ago.
I have posts(collection) -> userid(document) -> userposts(collection) -> postid(document) -> fields (postname,description).
How can I perform search based on postname? I want to get post from all users.
I have following and followers collection
following -> userid(document)->userfollowing (collection) -> all the user ids(documents).
followers -> userid(document)->userfollowers (collection) -> all the user ids(documents).
I have a timeline page the current logged in user should be able to see all the post from users to whom he has followed.
I am ready to change the structure if this is not correct.
You can use collectionGroup query with where():
final query = Firestore.instance
.collectionGroup('posts')
.where('postname', isEqualTo: 'post_name_to_search')
This will return document(s) from posts sub-collection of all users that matches the provided name.

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.

Fetch documents from multiple subcollections - Firebase [duplicate]

This question already has answers here:
Firestore query subcollections
(11 answers)
Closed 3 years ago.
I'm creating a web app for admin with firebase integration, this web app is used by admin to monitor posts or comments posted by user from Android or iOS app developed in ionic.
I'm using Firebase firestore, and following is the database design
posts/{postsDocument}/comments/{commentsDocument}.
The comments here is sub-collection for postsDocument that holds all the comments for a particular post. Also postDocument and commentsDocument contains Firebase uid of the user.
My problem is, whether it's possible to fetch all the comments commented by a particular user. I can query a single collection, but in this scenario commentsDocument is a subcollection and i want to fetch all comments across all the post by a user for monitoring purpose.
You can check firebase collection group query
var commentsQuery = db.collectionGroup('comments').where('commentOwnerID', '==', userId);//userId - firebase id of the user who commented
commentsQuery.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data());
});
});
Also you might have to create index that supports your query, check console for the the link for creating index

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

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

Resources