I have used FirebaseAuth for Login purpose in application. While createUserWithEmailAndPassword I am creating the same user in FirebaseDatabase with some additional fields.The structre is
[{
"email":"abc#gmail.com",
"password":"xyz",
"name":"sachin",
"address":"Pune",
"contact":"1234567890"
},
{
"email":"pqr#gmail.com",
"password":"def",
"name":"Ashay",
"address":"Pune",
"contact":"1234577777"
}]
Suppose after successfull Login of user Sachin with email abc#gmail.com and password xyz I want the address and contact from database.How to get that values ?
For future reference if you want to get a certain information from DB using UID
//To get logged in user information
var user = firebase.auth().currentUser;
var uid = user.uid; //get the UID from the current user
According to me, you should use firebase uid as index for storing data. When user authenticated you will get user's uid, on this basis you can access user's data. I hope it will work.
Related
I have a application which works on Firebase Database.After successfull Login of a user created in Firebase using Firebase Auth user is saving some data in Firebase.The requirement is I need to add the data POST by that user with some key to identify data.
[
{
"key":"Satyam",
"value":"some data"
},
{
"key":"Akash",
"value":"some data"
},
{
"key":"Yash",
"value":"some data"
}
]
Suppose user Satyam has login using credentials of Firebase Auth,so data POST by Satyam need to be added in database where key is Satyam. How to achieve this ?
I am new to Android programming. I am working on an app. When I signup in my app this shows the previously added data and does not ask me to add new user data to the database.
Even if I again login with a registered user then it again shows the previously added data.
I want that when I create a new user then a new node should create and then its child with data.
Database Picture
First of all, you need unique id to share your data in firebase. You can use user id for that. If you use firebase authentication. you can get user id from
FirebaseAuth.getInstance().getCurrentUser().getUid();
You can use this with email password firebase authentication. Likewise, you can get a unique id for authentication. Then u need to use that id as a key of your firebase database. Then your data structure should be changed like below. (key would be userids)
childsQuerity
|-------------adminInfo
|-------key1 -> value1
|-------asas1234 -> value2
|-------------childInfo
|-------key1 -> value1
|-------key2 -> value2
in your android activity use below command to push data to db
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("childInfo").child(userid);
ref.setValue(userDataUWant);
When creating a new user, get userid and update that firebase data like above-mentioned method. Then if an already registered user logged to the system. u can get user id using above mentioned method and you can get that user details as well.
So, I have a list with some users:
And a custom table with Username and a User UID reference:
After the user logged in I store his UID in a variable but, what is the correct way to retrieve the userName based on UserUID?
I recommend changing the structure of your database. Instead of using push() when you add users to the database, use setValue() with the userId as the key and the username as the value:
{
users: {
EsqhdtziWRPtoNLu485zvg: "Test User 1",
yBnNVVyHHcfq67ccGABGz2N8R: "Test User 2",
...,
...
}
}
This makes the username much easier to access.
I want to reach a user profile page. how can call a specific user with firebaseAuth. this code gives me only current user. how can call a user with user id.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
are there any code like this:
FirebaseUser user = FirebaseAuth.getInstance().getUser(mywantuserID);
why I want to use FirebaseAuth otherwise profile updates will take my time.
if you need an other user detail you need to store what you need at the firebase you can't use the auth method
I believe the FirebaseAuth object only has the capability to get the currently signed-in user's details.
Maybe you could create a User object in your schema and have the UID from the Firebase Users as the key:
users
---UID
------first_name
------last_name
------etc...
And then with that, you get a user's ID with a simple Firebase Query like:
FirebaseDatabase.getReference('users').orderByKey().equalTo(yourUID)
To get user uid, simple use user.geUid();, for example:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid());
Firebase documentations
I've used the Real-Time Database with this setup:
->users
->uid
->name
->email
->other info
If I wanted to save the user data I would use my User class and then set the object in the database like this:
//assume variables have already been declared...
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
User user = new User(name, email, other values...);
mDBRef.child("users").child(mFirebaseUser.getUid()).setValue(user);
I tried it and it works fine.
But how can I retrieve these values from the database? For instance, once I set a User object as shown above, I may want to retrieve the user's email. I don't mean getting the email through the sign-in provider. I want the email through the real-time database. I've tried working this out for a while now but the documentation isn't helping much. All it shows is to setup listeners in order to wait for changes to the data. But I'm not waiting for changes, I don't want a listener. I want to directly get the values in the database by using the keys in the JSON tree. Is this possible via the real-time database? If so, how can it be done because either the documentation doesn't explain it or I'm just not understanding it. If not possible, am I supposed to be using the Storage database or something else? Thanks.
Firebase uses listeners to get data. That is just the nature of how it works. Although, you can use a single event listener (the equivalent of just grabbing the data once). It will fire immediately and get the data, and will not fire ever again. Here's a code example to get the current user's email:
//Get User ID
final String userId = getUid();
//Single event listener
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
User user = dataSnapshot.getValue(User.class);
//user.email now has your email value
}
});