Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save references to Firebase Auth Users in a Firestore document?

So I have an application where you can register and login. I did that using Firebase Auth.

And then I have a Firestore where I have multiple collections like "Requests" and "Offers". A Firebase-Authed user in my App can create new Requests, which I add like this:

    firestore
        .collection('requests')
        .add({
            category: 'Test',
            customer_id: this.props.appState.user.uid, // The uid from Firebase Auth
            link: this.state.productLink
        })
        .then(docRef => {
            console.log('Successfully saved!');
        })
        .catch(error => {
            console.log(error);
        });

So as you can see, I'd like to store the users Firbase Auth uid to the request so I can query a request by a user later in my app.

The question is: Is that the correct way? At this point I don't want to create a users collection in my Firestore to hold more data for a user. In that case I could add a document with the uid as key and then in the "Requests" collection save a reference to the user. But in the current case I can't or at least I didn't find a way yet, reading the docs and searching google.

like image 712
SVARTBERG Avatar asked Feb 27 '18 10:02

SVARTBERG


People also ask

How do I reference someone on firestore?

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. This user is first on the weekly Google Cloud leaderboard.

Does Firebase Auth store user data?

User properties Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).

What is reference data type in firestore?

REFERENCE DATA TYPE IS ONE OF THE MANY DATA TYPES OF CLOUD FIRESTORE . THIS DATA TYPE ACTS LIKE FOREIGNKEY EVEN THOUGH IT IS NOT ACTUALLY A FOREIGNKEY . THE MAIN ADVANTAGE OF USING IT IS TO REFER A CERTAIN DOCUMENT TO ANOTHER DOCUMENT .


1 Answers

Giving this an answer to avoid people having to read through the comments conversation.

The optimal route is to create a collection called users where the document ID is the uid of the user from Authentication. Easiest thing is to do this on new sign up, you can do this where you handle new user registration or you can look into Cloud Functions which would look for new authentications and run the function like this:

exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
  // ...
});

Documentation here: https://firebase.google.com/docs/functions/auth-events

With or without the users collection, storing the UID like in the sample works fine as long as you always have a way of retrieving the user's information. Creating the users collection makes manipulating the types of data you store for a user much much more flexible (because there's zero flexibility with the standard auth objects, for good reason).

like image 119
Thingamajig Avatar answered Sep 19 '22 17:09

Thingamajig