簡體   English   中英

如何從firebase獲取另一個用戶數據?

[英]How get another user data from firebase?

我想當用戶點擊管理員個人資料時,用戶可以看到管理員信息。 我使用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);
        }
    });

我希望當用戶點擊管理員個人資料時顯示管理員信息不是當前用戶信息

您無法從客戶端代碼中查詢Firebase身份驗證中的其他用戶帳戶。 你必須要么

  1. 將其他用戶的數據存儲在數據庫中,並查詢該數據庫。
  2. 調用一些后端代碼並使用Firebase Admin SDK執行查詢,並將其返回給客戶端。

通常人們選擇選項1。

您可以創建Firebase cloud functions然后可以從Android應用程序調用該功能。接下來,在該功能下,您可以檢索相應用戶的數據並將其作為結果返回到您的Android應用程序。

創建雲功能並使用Firebase Admin SDK。

您可以通過uid或電子郵件或電話號碼獲取其他用戶數據。

並且您可以按最大1000列出每個用戶。

如果使用node.js創建雲函數,則代碼就是這樣。

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

看到:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM