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.
Save User Related DataOnce you have an authenticated user's unique identifier you can start to save documents in your Firestore with the UID as an identifier. Each time the user logs in you just look up the document in your Firestore to retrieve the related information about this user/login. });
Firebase Realtime Database is a NoSQL cloud database that is used to store and sync the data. The data from the database can be synced at a time across all the clients such as android, web as well as IOS. The data in the database is stored in the JSON format and it updates in real-time with every connected client.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With