FirebaseUser How to get a User from Uid - android

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

Related

FirebaseUser getCurrentUser bug. Always return previous user id

Using Firebase auth. So I need to get current user Id. If user1 logged out and user2 logged in it suppose to return me id of user2 because current session use user2 credentials. But for some reason if user1 logged out and user2 logged in when call 'getUid()' it returns me uid of previous credentials of user1
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
When logging out a user, you need to call the method signOut():
FirebaseAuth.getInstance().signOut()
When you call this, the id will change

Get data from Firebase Database with Specific node after successfull Authentication

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.

Can I change what information FirebaseUI asks for when a user signs up?

I am making an app with firebase auth functionality for signing in and signing up . When I was searching , I came upon FireBase UI which seemed good . When I saw the auth documentation , the create user method accepted only Email ID and password , but the Firebase UI gets the First and last name of the user too. What does it do with the name entered? Is it stored somewhere in the auth or database or is it just for showcase? If I implement my own UI for sign up , can I add more details than just email id and password ?
I am one of the developers of FirebaseUI.
In order to save the display name, FirebaseUI issues a UserProfileChangeRequest after sign in:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Jane Q. User")
.setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});
This allows for the storage of some basic fields like display name and photo URL. For custom fields, you will need to store the information in the Firebase Realtime Database.
https://firebase.google.com/docs/auth/android/manage-users#update_a_users_profile

Firebase Android, get only one key value pair

I am using Firebase Database in Android. In my app there are three type of users. one of them is "Driver" as shown in json tree below, I want that when user sign in, it automatically gets the value from key value pair "Role" so that I can start the respective activity. is there any easy way to do it or any way to do it?
Assuming you have checked that user is logged in (by Firebase Authentication) and random key child of Driver Information is user uid, then it should be like this:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase.getInstance().getReference("Driver Information/" + user.getUid() + "/Role")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String role = dataSnapshot.getValue(String.class);
// do someting with role
}
...
});
Note: replace addValueEventListener with addListenerForSingleValueEvent if you want to get the data one time only and don't mind if that data get changed.

How to get data from Real-Time Database in Firebase

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
}
});

Categories

Resources