1

I am trying to retrieve UID of all the users except of the current user. I tried to do FirebaseAuth.getInstance().getUid(); but it returns CurrentUserUid. Is there a way for NOT getting UID of the current user?

1 Answers1

0

When you are using your app, you are considered as a normal user and not an admin or privileged user. Client SDKs cannot fetch information about other users. You would have to use a secure environment such as Cloud Functions or your own along with Admin SDK to retrieve information about other users.

You can create a Callable Function that fetched a specific user by email or UID like this:

export const getUser = functions.https.onCall(async (data, context) => {
  // Verify if the user requesting data is authorized
  const {email} = data;
  const userRecord = await admin.auth().getUserByEmail(email)
  return userRecord.uid
});

You can then call this function from your Android app like this:

private Task<String> getUserInfo(String userEmail) {
    // Create the arguments to the callable function.
    Map<String, Object> data = new HashMap<>();
    data.put("email", userEmail);

    return mFunctions
            .getHttpsCallable("getUser")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or failure, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    String result = (String) task.getResult().getData();
                    return result;
                }
            });
}

Do note that you must check if the user calling the function and requesting the data is authorized. You can check custom claims, the UID or any identifier that decides who can view others users' information.

If your application requires anyone to view all information, then you would have to use any database that stores users' information and can be fetched on your app because you cannot fetch that data using Client Auth SDK.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Alright,thanks a lot for your kind response –  Jul 25 '21 at 13:28
  • @Tomas if my answer was useful you can accept (✔) it so others will know this was useful. Feel free to ask any further queries. – Dharmaraj Jul 25 '21 at 17:39