Customize IDs into Firebase Realtime Database [duplicate] - firebase

This question already has an answer here:
Post request to firebase without unique key
(1 answer)
Closed 3 years ago.
I'm using Firebase Realtime Database integrated in a mobile app and web panel. By default, a unique ID on Realtime DB is generated on this format: -Lup7ey2xRPO9Owm67xM
When creating new register through the Firebase panel, i can customize the unique id with GUID.
{
"0a921e40-5959-43bc-b2be-a65f6712288d": {
"name": "Test"
},
}
However, after call Rest API, with Postman or on my backend, the unique ID is generated by default on Firebase:
{
"-LupIEooOPXAzbc--dB_": {
"name": "Test 2"
},
}
Is it a possible configure Firebase do generate unique ids on format GUID? Replacing default format?

The documentation for the REST API says that using POST is the equivalent of a push operation, which always generates that random push ID. If you want to specify your own name for the node to add, you should be using a PUT request instead and using the client-generated ID in the path for the update.

Related

Firebase Auth REST API - Adding more fields to Sign up [duplicate]

This question already has answers here:
How to save extra info during user signup process using firebase authentication
(4 answers)
Adding extra information to registration user Firebase
(1 answer)
Closed 2 years ago.
I'm trying to add more fields to accounts:signUp e.i
fields needed: firstname surname phone...
https://firebase.google.com/docs/reference/rest/auth#section-create-email-password
so far I can pass the email and password and a user will be created, how do I add more fields? and store that information in a users collection in cloudfirestore?
Firebase Auth does not have a facility for storing arbitrary user data along with an account. You're supposed to use a database to store that, and link the record you store there to the user ID that Firebase Auth assigns to the user.

Fetch documents from multiple subcollections - Firebase [duplicate]

This question already has answers here:
Firestore query subcollections
(11 answers)
Closed 3 years ago.
I'm creating a web app for admin with firebase integration, this web app is used by admin to monitor posts or comments posted by user from Android or iOS app developed in ionic.
I'm using Firebase firestore, and following is the database design
posts/{postsDocument}/comments/{commentsDocument}.
The comments here is sub-collection for postsDocument that holds all the comments for a particular post. Also postDocument and commentsDocument contains Firebase uid of the user.
My problem is, whether it's possible to fetch all the comments commented by a particular user. I can query a single collection, but in this scenario commentsDocument is a subcollection and i want to fetch all comments across all the post by a user for monitoring purpose.
You can check firebase collection group query
var commentsQuery = db.collectionGroup('comments').where('commentOwnerID', '==', userId);//userId - firebase id of the user who commented
commentsQuery.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data());
});
});
Also you might have to create index that supports your query, check console for the the link for creating index

How to create already made collections when i register a user account on firebase

I'm using flutter with firebase, when i register an account into firebase how do i get a .collection('userData').document(uid)
.collection('bio') already make with that account, what i want to do is, when i create the account, some data is already in the 'bio' documents for me in the cloud Firestore such as:
name: ""
age: ""
date: ""
height: ""
and so on, then all i have to do is fill out those fields to show on my app, i'm doing this for all the students in my school, and i can do it manually but that will take way to long since its way over 5,000 students, or is it even possible to copy the .collection with one array of data such as the one above and paste in into each user 'bio' document and go from there with each of them instead of having to keep adding fields for each one.

Custom Authentication in Google Firebase

I have a question regarding authentication using Google Firebase.
For an app, I want to build an authentication similar to the one Slack uses: first, the user provides the input as to which group they want to log in to. If there exists a group with the same name as provided in the input, the user is then taken to a login/signup screen.
I've thought about storing users in the realtime database as follows, but I think there must be a better way to do this (since I don't think I can use the firebase authentication in this case):
groups: {
"some_group_name": {
"users": [
"user1": {
.. user 1 information
},
"user2": {
.. user 2 information
}
],
"group_details": {
"name": ..,
"someGroupDetail": ..
}
},
"some_other_group_name": {
...
}
}
I haven't realized if there is an obvious answer yet, so I'm open to suggestions. How would you suggest I tackle this?
Thanks
PS: I'm building the application using Nativescript and Angular, and (so far) there is no server or database involved other than Firebase.
Another suggestion that might work, is by using Firebase Auth Custom Claims. That way, you only need to store the group ID and group name in your realtime database, without worrying to keep changing the database each time user is added or removed.
This is one way you can do it:
Store database exactly like you have it, with it's group ID and name.
In your backend script (I recommend Cloud Function), each time a User is registering themselves, add custom claims in your user: Specifying what group is the User belong to.
Every time user authenticate, retrieve the group ID from custom claims. And there you get it!
Note: be careful not to put too much information in your custom claims as it cannot exceed 1000 bytes.
Read more about it here: https://firebase.google.com/docs/auth/admin/custom-claims
I would suggest you to implement Root-level collections.
Which is to create collections at the root level of your database to organize disparate data sets(as shown in the image below).
Advantages: As your lists grow, the size of the parent document doesn't change. You also get full query capabilities on
subcollections.
Possible use case: In the same chat app, for example, you
might create collections of users or messages within chat room
documents
Based on the reference from the firebase cloud firestore
Choose a data structure tutorial (I know you are using Realtime database but structuring the database is the same since both are using the NoSQL Schema)
For your case:
Make 2 Collections: Users, Groups
Users: User info is stored in the form of document
Groups: In the Groups Collection, here comes the tricky part, you can either store all groups subcollection under 1 document or split into multiple documents (based on your preference)
In the group-subcollection, you can now store your group info as well as the user assigned where you can store user assigned in the form of array, therefore whenever a user access the group, query the user assigned first, if yes, then allow (assuming users can view all group)
You do the thinking now

firebase realtime db checking a record for exist [duplicate]

This question already has answers here:
Test if a data exist in Firebase
(3 answers)
Closed 4 years ago.
I am using angularfire2 wth my ionic app and have sign in with facebook enabled.
When first time a user logs in using the fb then i naturally dnt have any record of it in my firebase DB. I want to create a user node using the facebook returned uid as the node.
The way i can retrieve it is like below. I can check by seeing if it has child objects etc. But is that the way recommended to do this kind of checks? I also don't see how to add subscribe or then to it as i would assume this should be async operation for me to wait until it confims me that user does not exist yet. Please advise
const userRef = this.db.object("/users/" + uid)
Simply building a reference to some location in the database (as shown in the code above) says nothing about whether or not data exists at that location. The location needs to be queried in order to figure out of data exists there.
The sample code get from firebase web API document.
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
updateStarCount(postElement, snapshot.val());
});

Resources