【问题标题】:How to retrieve UID of other users and not the current user? [duplicate]如何检索其他用户而不是当前用户的 UID? [复制]
【发布时间】:2021-10-01 17:54:34
【问题描述】:

我正在尝试检索除当前用户之外的所有用户的UID。我试图做FirebaseAuth.getInstance().getUid();,但它返回CurrentUserUid。有没有办法不获取当前用户的 UID?

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    当您使用您的应用程序时,您被视为普通用户,而不是管理员或特权用户。客户端 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 获取该数据。

    【讨论】:

    • 好的,非常感谢您的热情回复
    • @Tomas 如果我的回答有用,您可以接受 (✔),这样其他人就会知道这很有用。如有任何进一步的疑问,请随时提出。
    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多