如何为不同类型的用户使用firebase身份验证来访问各自的实时数据库节点或子节点

如何为不同类型的用户使用firebase身份验证来访问各自的实时数据库节点或子节点,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,我正在使用Firebase构建一个电子商务android应用程序。我如何在Firebase身份验证中隔离不同的用户,如客户和卖家,并在Firebase database Realtime database中授予对其各自数据库节点的读写访问权这可以通过使用类似以下数据库结构实现: { "users": { "someUserId-782nafdca9": { "name": "Joe Smith", "type": "cust

我正在使用Firebase构建一个电子商务android应用程序。我如何在Firebase身份验证中隔离不同的用户,如客户和卖家,并在Firebase database Realtime database中授予对其各自数据库节点的读写访问权这可以通过使用类似以下数据库结构实现:

{
    "users": {
        "someUserId-782nafdca9": {
            "name": "Joe Smith",
            "type": "customer",
            ...
        },
        "someUserId-78sdfgs523": {
            "name": "Example Supplier Co.",
            "type": "seller",
            ...
        }
    },
    "dataForSellers": {
        ...
    },
    "dataForCustomers": {
        ...
    }
}
遵守以下规则:

"rules": {
    "users": {
        "$uid": {
            ".read": "auth.uid == $uid",
            ".write": "auth.uid == $uid",
        }
    },
    "dataForCustomers": {
        ".read": "auth != null && root.child('users').child(auth.uid).child('type').val() == 'customer'",
        ".write": "auth != null && root.child('users').child(auth.uid).child('type').val() == 'customer'"
    },
    "dataForSellers": {
        ".read": "auth != null && root.child('users').child(auth.uid).child('type').val() == 'seller'",
        ".write": "auth != null && root.child('users').child(auth.uid).child('type').val() == 'seller'"
    }
}
安全规则有许多可用于检查各种条件的规则。上面的规则使用auth和root变量

users/$userId下的用户数据规则当前仅检查当前用户是否与他们试图修改的用户数据匹配auth.uid==$uid

dataForCustomers和dataForSellers的规则首先检查用户是否登录了auth!=null,然后检查root.child'users.childauth.uid访问的用户数据是否包含类型的正确值

注1:正如他们在评论中所提到的,可以在安全规则文档中找到关于的更多详细信息

注2:与官方文档一样,此示例并非没有缺陷。例如,在使用上述安全规则的任何时候,如果用户登录,他们可以:

如果客户和卖家登录到您的数据库,则在他们之间进行更改。 将垃圾添加到数据库中 删除其他用户制作的任何数据,无需考虑作者 注3:不要只将所有数据嵌套在dataForSellers和dataForCustomers下,它们是作为您自己的数据树(如产品、装运、订单等)的占位符名称提供的


注4:由于您刚刚开始使用RTDB,请务必阅读本文。对于电子商务,您最好使用Firestore,因为Firestore只进行过滤。

您可能需要从文档开始,特别是关于的部分。回答得好,Sam!