This question already has answers here:
How do I link each user to their data in Firebase?
(2 answers)
Closed 3 years ago.
In case of Login and Registration app, how can we store the details of each user uniquely in firebase real-time database? when I try to login with a different user the data is being overwritten.
How can I Solve This?
If you are logged in with a user with FirebaseAuth you can use getUid() to get a unique identifier for that logged in user, then you can write to the database with that UID, the next time a user logs in with the same account, it will write on that same unique destination. If the user logs in with another account, another new unique identification will be created for that specific account.
Example
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance().getCurrentUser();
String userID = mAuth.getUid();
mDatabaseRef.child("semOneOne").child(userID).child("name").setValue("Gastón");
Important: Sometimes getCurrentUser() could be null, and
so on your userID, so, make sure you check if the user is logged in
first before getting the userID
For more information please check on how to Manage Users in Firebase
Related
When the user logs into my app, I want to store his UID somewhere in the app because I do certain offline functionalities with the UID. My requirement is that, once he logs in, I should be able to store the UID somewhere by retrieving it from firebase. Once that is done, unless he logs out, I need to be able to access this UID locally on the app to certain processes from various different activities. Only when he logs out, this UID can be deleted. If he quits the app, or the internet connection is lost, my ability to access this UID string shouldn't be affected. What is the best place to store this UID?
Firebase Auth already takes care of keeping the UID at all times for you. You can access it using the code showed on the documentation:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is logged in
String uid = user.getUid(); //this is the stored uid
} else {
// User is logged out
}
I am new to Firebase and I assumed the user id generated by Firebase Database is the same one I get by using currentUser.getUid(). But as you can see, those two values don't match for my current user.
I can't figure out the reason. Please help. How to solve this?
As i can see in your screenshots you are using two different ways for having that unique key, a uid provided by the firebaseUser object and the unique key generated by the push() method. It's obvious that are not the same, becase there are different principles.
The first one is the uid provided by the Firebase authentication and the second by the push() method, that generates a unique id based on the time of the generation.
The best way for using as an identifier for your users is not the uid, nor the pushed id, is the email address, because if a users deletes his account and sign-in again, he'll gets a new uid.
If you want to use the uid, this is the way in which you can retrieve it:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String uid = firebaseUser.getUid();
}
Hope it helps.
Those are totally different things. ID that you get with getUid() is an ID that is created when user is signed up to Firebase Authentication. Other one is key created based on the current timestamp in the Firebase Database when you call push() method. What you could do is instead of creating new person in your database with push method, use the id that you get from getUid()
This question already has answers here:
How do I return a list of users if I use the Firebase simple username & password authentication
(7 answers)
Closed 6 years ago.
I am storing user's display name in Firebase Auth. Fetching from Firebase Auth is simple when the same user is logged in. But, when another user is logged in, how do I fetch an account's display name when I have their unique UID (User ID).
One solution is when you register a user add his details in database also
Example
Once you are under createUserWithEmailandPassword()
DatabaseReference db=FirebaseDatabase.getInstance().getReference();
DatabaseReference users = db.child("Users").child(mAuth.getCurrentUser().getUid());
users.child("Name").setValue("<NAME>");
Now when the user signs into your database retrieve the Name from the database from child Users
You can't get the name, or any other details of a user that is not currently signed in using FirebaseAuth.
Instead, you must create a node in your database where you store the name, and any other necessary details by querying the database.
I'm really curious to know how you have the UID of any other user without doing this.
I am currently making an Android app that allows Facebook sign-in and for each user, stores their email address (from Facebook) along with a couple integer variables in my database.
Upon login, I want my app to check if my database contains the email address of the logged on user. If it does, I proceed with the app functionality, if not, I create a new entry in my database for that user, with an email address and the associated integers.
How can I formulate a query or request that searches my database for a given email string? I am not very experienced with Firebase.
Using FirebaseAuth for your Facebook sign-in would give every user a unique id generated by Firebase.
This id is returned to you in the code when login is successful. You could then retrieve the user's email from the returned object.
Assuming you're using Firebase's real time database to store your info, you could check out their official docs here on how to use your DatabaseReference DataSnapshot to query the database
The Firebase Docs here say the following:
A Firebase User instance is independent from a Firebase Auth instance. This means that you can have several references to different users within the same context and still call any of their methods.
QUESTION: How to get/create FirebaseUser instances for other users? I got an admin user who can change profile info of other users, like photo url or display name.