【发布时间】:2021-10-01 17:54:34
【问题描述】:
我正在尝试检索除当前用户之外的所有用户的UID。我试图做FirebaseAuth.getInstance().getUid();,但它返回CurrentUserUid。有没有办法不获取当前用户的 UID?
【问题讨论】:
标签: java android firebase firebase-realtime-database
我正在尝试检索除当前用户之外的所有用户的UID。我试图做FirebaseAuth.getInstance().getUid();,但它返回CurrentUserUid。有没有办法不获取当前用户的 UID?
【问题讨论】:
标签: java android firebase firebase-realtime-database
当您使用您的应用程序时,您被视为普通用户,而不是管理员或特权用户。客户端 SDK 无法获取有关其他用户的信息。您必须使用 Cloud Functions 或您自己的安全环境以及 Admin SDK 来检索有关其他用户的信息。
您可以创建一个Callable Function,通过电子邮件或 UID 获取特定用户,如下所示:
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
});
然后您可以像这样从您的 Android 应用中调用此函数:
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;
}
});
}
请注意,您必须检查调用该函数并请求数据的用户是否被授权。您可以检查自定义声明、UID 或任何决定谁可以查看其他用户信息的标识符。
如果您的应用程序需要任何人查看所有信息,那么您将不得不使用任何存储用户信息并且可以在您的应用程序上获取的数据库,因为您无法使用 Client Auth SDK 获取该数据。
【讨论】: