如何从 firebase 获取另一个用户数据?

Ali*_*omy 5 android firebase firebase-authentication

我希望当用户单击管理员个人资料时,用户可以看到管理员信息。我使用 currentUser 来获取已在应用程序中登录的当前用户的 id。

我想知道如何获取另一个用户数据。

    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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我希望当用户单击管理员个人资料时显示管理员信息而不是当前用户信息

Dou*_*son 6

您无法从客户端代码查询 Firebase 身份验证中的其他用户帐户。你必须要么

  1. 将其他用户的数据存储在数据库中,并查询该数据库。
  2. 调用一些后端代码并使用 Firebase Admin SDK 执行查询,并将其返回给客户端。

通常人们会选择选项1。


zko*_*ohi 4

创建 Cloud Functions 并使用 Firebase Admin SDK。

您可以通过 uid 或电子邮件或电话号码获取其他用户数据。

您最多可以列出 1000 个用户。

如果您使用 node.js 创建 Cloud Functions,则代码如下所示。

// 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;
  }
});
Run Code Online (Sandbox Code Playgroud)

看: