This question already has an answer here:
Add additional data to user profile via firestore functions onCreate
(1 answer)
Closed 2 years ago.
I have two different applications; let's call them A and B. I use Firestore to store additional user data under a collection called /users.
I have a cloud function that aggregates data whenever a user registers through Google, manual or Facebook to the users collection.
functions.auth.user().onCreate( (user, ctx) => {
//aggregate data in /users
});
The problem is that I want to add another collection, say different-users that are only added when using application B. According to Firebase documentation:
You cannot add other properties to the user object directly; instead,
you can store the additional properties in any other storage services,
like Google Cloud Firestore.
How am I supposed to differentiate these types of users in my cloud function giving the fact that I can't add additional data?
Am I missing something?
The user is created in Firebase Authentication and that is what triggers your Cloud Function. At the point your backend code gets invoked, there is no information anymore about what app called the authentication API.
If you want to store additional information based on where the user was created from, consider calling a Callable Cloud Function directly from within your application code after the creation of the user in Firebase Authentication completes. Alternatively, you could write the info directly to the database from the client, if that works for your use-case.
Also see:
Add additional data to user profile via firestore functions onCreate (which I now see is a duplicate, so I'll close against it in a minute)
Firebase Cloud Function - Create user with extra information
Related
I have a step in my new user flow where a user is registered with Firebase auth, and then a record is written to Firestore.
Is there a straightforward way to put both the user registration and the firebase write into a transaction, so if either one fails, they both fail?
Is there a straightforward way to put both the user registration and the firebase write into a transaction, so if either one fails, they both fail?
There are no Firebase products that support cross-product transactional operations. You'll have to nest both calls and always handle the errors. So do the authentication, and then write the data to Firestore or the Realtime Database.
A more convenient way of handling this situation would be to trigger a Cloud Functions in response to the creation of a Firebase user account. In this way, you will only need to handle a single operation on the client.
I want to have a collection with all the users and I want to store their current location.
I will have a function that is calculating the distance from the current user location to the location of each user that is in the collection and I want to receive only the users that are X kilometers away from the current users.
I can get all the users and filter them on the front-end but wont that be a lot of document reads if there are a lot of users ?
Is there a way to pass the current user location to firebase and do the filtering on the server side ? Or the only way is to get all the users and filter them on the front-end ?
If you want to avoid doing large amounts of work in your client app, you can use Cloud Functions for Firebase to create a backend API endpoint and push that work to a hosted backend. A full discussion of a possible implementation is too long for a Stack Overflow answer, but you will want to read that documentation to get started building a backend. One easy approach is to write a callable function that you can invoke directly from the client. The function can then use the Firebase Admin SDK to query the database, perform some computations, and return the results to the app.
my app is fetching data from a Firestore database but this data should only be accessible to users who are currently subscribed to In-App-Purchases.
What is the best way to protect this data?
My first thought was routing everything through a Firebase Function, so the user has to send their subscription ID to the function, the function checks it and returns the data, but this would require way too many function calls for just a few users.
If you are not using any server client libraries then you can create a rule on firestore to restrict the reads on the table.
firestore rules docs..
You could simply check if the requesting user has subscribed to In-App-Purchases (assuming there is a flag in the user's data to confirm the same).
Is it possible to reference user objects in Firestore like a regular document from collection ? I've created a sample of fake users for test in the Authentication section, and I'd like to reference them in a collection.
Their is the reference field :
The id seems correct, however calling a db.doc() with this reference as a parameter returns an empty document (snapshot.exists returns false).
The database 'users' isn't present in my collections since it is handled by Firestore authentication system, but I was wondering if their was a way to access it, similar to regular documents.
You seem to be mixing up two products in the Firebase platform:
Firebase Authentication handles user sign-in.
All information about these users is stored in an internal database,
that you can only access through the Firebase Authentication APIs.
Cloud Firestore stores data that you put in it.
Firebase Authentication does not automatically create any user data in Firestore when a user is created. If you want such data in Firestore, you'll have to create it yourself, either in your application code, or in Cloud Functions in response to the user-created event.
Within Cloud Firestore there is no type that is a "reference to a Firebase Authentication user". But if you store user-specific documents in Firestore, you can use its Document Reference type to reference those user-specific documents.
I need to check a document's data in Firestore to allow someone to view an image in Firebase Storage. Is this possible?
It is currently not possible to access Cloud Firestore documents directly from Cloud Storage rules. You have two options:
Somehow use Firebase Authentication custom claims on the user account to determine if a user should be able to access a file.
Use some backend code (maybe a Cloud Functions HTTP trigger) that the user accesses to download the file if the conditions are met.
In either case, you will need some backend code that checks and sets the appropriate data.