Ali Qadomy
Ali Qadomy

Reputation: 151

How get another user data from firebase?

I want when user click to admin profile, user can see the admin info. Im using the currentUser to get the id of current user who logged in already in applications.

I'm trying to know how get the another user data.

    String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();


    // init firebase database
    mUserDatabaseReference = FirebaseDatabase.getInstance().getReference("Users");

    mUserDatabaseReference.child(currentuser).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


            // here using Picasso for get the image url and set in ImageView
            String imageUrl = dataSnapshot.child("profile_pic").getValue().toString();
            Picasso.with(AboutCompany_for_users.this).load(imageUrl).into(companyPhoto);


            String name = dataSnapshot.child("name").getValue().toString();
            String email = dataSnapshot.child("email").getValue().toString();
            String phone = dataSnapshot.child("mobile").getValue().toString();
            String busesNumbers = dataSnapshot.child("buses_numbers").getValue().toString();
            String lineName = dataSnapshot.child("bus_line").getValue().toString();


            // here we get the data from
            companyName.setText(name);
            companyEmail.setText(email);
            companyPhone.setText(phone);
            companyBusesNumbers.setText(busesNumbers);
            companyLineName.setText(lineName);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    driversInformations.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent driversInfo = new Intent(AboutCompany_for_users.this, CompanyDrivers.class);
            startActivity(driversInfo);
        }
    });

I expect when user click on admin profile show the admin info not current user info

Upvotes: 5

Views: 5381

Answers (3)

zkohi
zkohi

Reputation: 2520

Create a Cloud Functions and use the Firebase Admin SDK.

You can get another user data by uid or email or phone number.

And you can list user each by max 1000.

If you create a Cloud Functions using node.js then code is like this.

// get another user data by uid
exports.getUser = functions.https.onCall(async (data, context) => {
  try {
    // if you want to deny unauthorized user
    // - context.auth.token.xxx is customClaims. (if you use)
    // if (context.auth || context.auth.uid || context.auth.token.xxx) {
    //   return Promise.reject("unauthorized");
    // }

    const user = await admin.auth().getUser(data.uid);
    return {
      displayName: user.displayName,
      email: user.email,
      photoURL: user.photoURL
    };
  } catch (error) {
    // If user does not exist
    if (error.code === "auth/user-not-found") {
      return {};
    }
    throw error;
  }
});

See:

Upvotes: 5

Sushant Somani
Sushant Somani

Reputation: 1500

You can create a Firebase cloud functions and you can call that function from your Android app Next, under that function, you can retrieve the data of the corresponding user and return it as a result to your Android app.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317372

You can't query other user accounts in Firebase Authentication from client code. You will have to either

  1. Store the other user's data in a database, and query that database.
  2. Call some backend code and use the Firebase Admin SDK to perform the query, and return that to the client.

Usually people choose option 1.

Upvotes: 6

Related Questions