10

How can I save additional user data in Cloud Firestore database? I mean, which one is the proper way? I want to create a user collection, where I can store their username and other data. I already found answers about this problem, but all of them are using the "old" Firebase Realtime Database solution.

Now I'm doing this way (this is a Vue component):

methods: {
    doSignUp () {
      if (!this.email || !this.password) {
        return
      }
      firebase.auth().createUserWithEmailAndPassword(this.email, this.password)
        .then(data => {
          this.createUser(data)
        }).catch(err => console.log(err))
    },
    createUser (userObject) {
      const docRef = this.$store.getters.getDB.collection('users')
      const username = userObject.email.split('@')[0]
      docRef
        .doc(username)
        .set({
          id: userObject.uid,
          username
        }).then().catch((err) => console.log(err))
    }
  }

In the example above, I just simply creating a new document in the "users" collection, and save the user's ID as "id". Is this the correct way? I'm not sure about it.

pkovzz
  • 353
  • 2
  • 5
  • 13
  • In your example "username" might not be unique – tmg Mar 07 '18 at 10:13
  • Yes, I know, thanks for the comment. It is just a demo app to learning Vue and Firebase. – pkovzz Mar 07 '18 at 10:23
  • that was exactly my approach. You end up with 2 list of users and you need to be aware that if the user wants to remove his profile you need to remove him from 2 places but apart from that it's ok – Dani Oct 13 '18 at 17:31
  • you also need to check if the user exits already – Dani Oct 13 '18 at 17:37

2 Answers2

4

I think there's nothing bad in your example. Of course as long as you set up proper security rules on your firestore database :)

Another thing is that IMO there's no difference in using the Realtime Database and the Firestore one. The differences are in the architecture and how it works "behind the scenes", but the overall usage - this is very similar (the API is just slightly different AFAIK).

One alternative solution that I could recommend you is to do the database write stuff outside your web app using Firebase Functions and the Firestore Triggers - https://firebase.google.com/docs/auth/extend-with-functions This solution would remove the necessity of the createUser function on the client-side.

sarneeh
  • 1,320
  • 1
  • 12
  • 27
  • 2
    I overlooked these "cloud functions", thanks for your suggestion! – pkovzz Mar 07 '18 at 10:38
  • @sarneeh `"Of course as long as you set up proper security rules on your firestore database :) "` And what would these proper rules be? Cause at this point, no user are authenticated. So how can you save the user doc in a safe/secure way? (except for using the trigger...) – Vegar Apr 04 '19 at 07:49
  • @Vegar I'm not very experienced with Firebase, but what I'd consider is using Anonymous authentication: https://firebase.google.com/docs/auth/web/anonymous-auth – sarneeh Apr 04 '19 at 08:20
  • I'm all new, but followed an tutorial that did what @zedz01 suggests in the question, but hey also said you should protect the database against anonymous access, to their code didn't really work... Your suggestion might work, but it doesn't sounds like the way it is supposed to be done. Like your trigger/function way better. Will look into that. – Vegar Apr 04 '19 at 08:28
  • hum... according to docs, the user should be signed in after `createUser...` is called, so there has to be something else wrong, I guess... – Vegar Apr 04 '19 at 10:25
2

Yes this is the correct way, you will have a collection called Users inside that collection there will be a document which is this doc(username) and has the value id: userid

You can also save the userid as an object, and inside that object you will have values related to that useruid

like this example:

var frankDocRef = db.collection("users").doc("frank");
frankDocRef.set({
    name: "Frank",
    favorites: { food: "Pizza", color: "Blue", subject: "recess" },
   age: 12
 });

https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134