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.
Related
Using R, I am trying to read the Firebase authentication records created by users for our project, projectID. To be clear, I do not wish to read, nor can I read, the user pswd, just the Firebase authentication ID, known as the User UID.
Firebase creates a unique UID when a user record is created. This in turn populates a collection, call it collectionID, where the key to collectionID is the Firebase authentication User UID. Other information about the user's use of our application is stored within this collection.
I can read the collectionID using the Firestore library created by Luis Rodriguez - github("luizmirodriguez/FireStore") - as follows:
fireStore.download(projectID,collectionID,auth$credentials$access_token).
I cannot download the authentication records shown in Firebase Console with R today. Does anyone know how to do this?
It may be as simple as knowing the name of the collection (authentication, authentication/users, authentication/users_uid all produce errors).
Any help appreciated
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
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.
I am working on an app using Firebase Auth and Firestore where we store user information in the /users collection. Some users login and have a Firebase Auth record. Other users don't login, their /users documents are just used as reference data.
For Firestore documents in the /users Collection that link to Admin records, I am setting the Document ID to the Firebase Auth UUID (28 character ID generated by Firebase Auth). This lets me easily fetch the user information after the user logs in.
For the others, I am creating the user records with the auto-generated 20 character ID Firestore creates via .doc() or .add(newData).
Is there any documented risk to this or has Firebase stated anything that makes this unwise? I have found nothing in their Guide/Reference documentation that says one way or the other. Their documentation states you can set the ID or let it be generated for you, but it never says you can mix the two methods in the same collection. Has Firebase stated any guarantees for the length of their IDs (currently Auth uses 28 characters and Firestore uses 20 which guarantees they'd never overlap). Will Firestore auto-create a new ID if the auto-generated ID already exists?
The biggest risk I see is if the implementation changed in the future and both had the same length IDs but I have been unable to find any guarantees one way or the other.
I like to use firebase functions to test firebase rules I defined.
I would like to read/write to realtime database as an existing user to test if the rules work as expected.
I read in getting started page, I can write to realtime database as admin as follow:
admin.database().ref('/messages').push({original: 'some text'});
How can I do the same as a user I have created in firebase instead of as admin?
I believe when you get the Delta Snapshot from the triggered event that the current state of that snapshot is tied to the user. Since the Firebase team is providing you with the server-less environment, they attach the admin as well since it's in a secure location.
So just grabbing the ref from the current snapshot, should give you the ability to test the database rules. Just to clarify, I am talking about the snapshot.ref, and not the snapshot.adminRef.
Here is the reference from their documentation:
Returns a Reference to the Database location where the triggering write occurred. This Reference has the same end-user permissions as the client that did the write. So, if an unauthenticated client did the write, this Reference is unauthenticated. If the client that did the write is authenticated as a certain Firebase Auth user, this Reference is authenticated as that same user.