I am developing an android application where older kids can pick up younger kids and walk to school. With the application, the authenticated (email and password) younger kid can choose between three addresses to get picked up. I have a user node and an address node.
The problem is that every time a user picks an address, the username child is overridden. Therefore there will only be one username per address.
My database structure looks like this:
database.child("Addresses").child(m1.getStreet()).child("username").setValue(name);
database.child("Addresses").child(m2.getStreet()).child("time").setValue(time);
database.child("Addresses").child(m3.getStreet()).child("address").setValue(address);
I thought about using the push function, but I don't think it can be used in this context. Any ideas?
setValue() will override everything in the existing map. Use updateChildren() if you want to add/update values and keep the old data.
If you have unique usernames or if you can get the keys in User, you can directly use that as a key:
database.child("Addresses").child(m1.getStreet()).child("users").child(name).child("username").setValue(name);
database.child("Addresses").child(m2.getStreet()).child("users").child(name).child("time").setValue(time);
database.child("Addresses").child(m3.getStreet()).child("users").child(name).child("address").setValue(address);
Related
I have done phone authentication linked to my project but I am not pushing the data from android instead I have manually entered the user data in Firebase. And I want to retrieve the data from firebase using OTP verification and I just need to search the specific user data with phone number which I am entering while login. I want to show all fields to be displayed which are present in my firebase database into my android project.
I've tried many methods to receive the data but I am stuck... Please help me.
Database structure
Authentication panal
You need to logically bind your authenticated user and the data that you stored in your firebase. As per the image in your data base 'id' of the 'Users' are 1,2,.. like that. Instead of that you can use 'UID' of users as an an Id in your database. Go to your 'Authentication' section, you will see User UID against each registered user. Copy that and use as an ID while creating data in database. This will help you to get the data with reference like Users\. Hope this will solve your problem.
I want the application to create a unique 4-digit code and save it to the firebase database. This code will not change when the app is exited and entered again.How do I do that?
Example Image
Starting talk about the combinations.. this type of code (4-digits) can give life to only 10.000 users. If your app has 10.001 user, probably it will crash or will be unusefull.
Avoiding talk about the combinations, instead, firebase can't do this with its API or premade functions. You need to implement this with you code, in the android app or (recommended) in some "Functions". If you don't need that this code is random, you can have a field on your database that do as a counter.
Every time an user need that his code to be created (for example on registration), you simply retrieve the counter value on the db and increment, assigning it to the user code field. In this way, the first user of your app will be the code 0000. The next, will have 0001,0002,etc.. The last user that can be registered will have the code 9999.
As said above, retrieving the user data from database after the registration, you'll have the previous saved user code too, so it will not change.
If you need more help, please tell us more about your app or scope.
I am making an app, and in that app, users login and I am storing their information; however, I have noticed that I don't have a users' password information after they register. Is it a good idea to store users' password when they register through Firebase? And is there a point where I will need their passwords? I want to make sure before I proceed further. Thanks!
You do not do that.
Use the (awesome, amazing) Firebase authentication system.
Click right here:
on the left, to see all the users - click "Authentication".
You never see / you cannot see their passwords.
You don't handle or touch the passwords at all.
In the Android or iOS app, you get the userid - and that's it.
The answer by #PeterHaddad shows perfectly how to do that.
That's the first and most basic step in any Firebase ios/droid app.
In your data you'll have a "table" called probably "userData/" and that's where you keep all data about the user. (For example, you may store their address, real name, shoe size .. whatever is relevant in your app.)
Note - FBase is so amazing, your users can also connect with other methods (phone, etc). For your reference in the future, that is explained here
You don't need to store the password in the firebase database, after you authenticate the user using createUserWithEmailAndPassword the email and other info will be stored in the authentication console. So you do not need the password in the database, all you need is the userid to connect auth with database.
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
I have two different types of users Teachers and Students . I use Firebase Auth with Email and password to Authenticate them and store them in the Firebase Real time database . My question is is there a way to create custom accesor methods such as getCurrentUser().getEmail, getDisplayname etc . I need to display different UI for different user type (Teacher/Student) from the current user-type
You got 2 type of users. So first of all, you can make only one firebase structure for both user and just add a boolean variable TEACHER that indicates if a user is a teacher or a student.
But what I usually do is to seperate the users in two different firebase structures meaning that you should create a firebase path teacher/user_id and an another student/user_id which will give you flexibility with retreiving data and display the data in different UI.
I am using firebase as backend for storage and authentication for the android app. I have created the authentication part. So, far it works well. The problem comes in database part.
My question here is:
I want to store list of names and address of client in the firebase database. How can it be done (Considering I have just a text file with 2 columns)? Can this be automated or should it be done manually? i.e. By creating a Location child and in that writing Name with name of client and Value as address. As the list is high is number how can this be automated?
Once the list is stored on database, I want to distribute specific number of address to specific users only. How can this be done through firebase? Is there any way to distribute say "first 10 names and address to user1" and next 10 to user2" and so on?
Fetch the address and names from database to the users as list and "feed it to Geolocation for validation" and "obtaining latitude and longitude" and "further sending it to Maps" for navigation.
That's it. This is the part I am struggling with.
Can you suggest an alternative to implement this?
Here are the answers.
(i) If you can convert your text file into a JSON formatted file, then you can upload it very simple using your Firebase console -> Database -> Import JSON. The structure must be as a pair, of key and value.
(ii) Yes you can do it. You need to create a Map for names and a Map for address like this:
Map<String, Object> map = new HashMap<>();
map.put("name1", name1);
map.put("name2", name2);
........................
map.put("name10", name10);
yourRef.child(userId).child("names").updateChildren(map);
"name1", "name2" ... "name10" can be also changed with ids. You can use a method named push() which generates unique keys. Also userId is the id of the specific user for which you want to assign the names.
(iii) Yes, you can achieve that too. You can take a look at this post.
Hope it helps.