[英]how to query from firebase realtime database with security rules applied
我已经为这个问题设置了 firebase 实时数据库的基本安全规则。 在我跳到问题之前,我已经使用 GetX 创建了一个 Flutter 移动应用程序,并且我还安装了http: ^0.13.4
插件用于查询。
这是我设置安全规则的方式
{
"rules":{
".read" : "auth.uid !== null && auth.token.email_verified == true",
".write" : "auth.uid !== null"
}
}
所以基本上,我希望用户在经过身份验证和 email 验证后能够读取数据。
对于写入规则,仅当用户通过身份验证并且 isAdmin 的自定义声明设置为 true 时。 对于isAdmin,我不知道要在安全规则中设置。
是这样的吗?
"auth.uid !== null && root.child('isAdmin').val() == true"
无论如何,我的问题是如何在客户端使用 http 插件进行查询? 这是 GET 查询的示例
Uri url = Uri.parse('URL HERE with /.json file');
getIPTIdAndName() async {
iptList.value = [];
await http.get(url, headers: {"Authorization": FirebaseAuth.instance.currentUser!.uid}).then(
(value) {
var response = json.decode(value.body) as List<dynamic>;
for (var i = 0; i < response.length; i++) {
iptList.add({
"iptIndex": i,
"iptId": response[i]['id'],
"iptName": response[i]['name']
});
}
iptList.refresh();
},
);
}
如何查询是否设置了安全规则?
更新代码
//create a function inside authController and called it inside the javascript
Future<String> getIdToken() async{
return await FirebaseAuth.instance.currentUser!.getIdToken();
}
getIPTIdAndName() async {
iptList.value = [];
Uri url = Uri.parse('$urlString?auth=${await auth.getIdToken()}');
await http.get(url).then(
(value) {
var response = json.decode(value.body) as List<dynamic>;
for (var i = 0; i < response.length; i++) {
iptList.add({
"iptIndex": i,
"iptId": response[i]['id'],
"iptName": response[i]['name']
});
}
iptList.refresh();
},
);
}
//security rules
{
"rules":{
".read" : "auth != null && auth.token.email_verified == true",
".write" : "auth != null && auth.token.admin == true"
}
}
如果 Firebase 接受 UID 作为 URL 参数,那将是非常不安全的,因为任何人都可以传递您的 UID 并获得对您数据的访问权限。
相反,您应该传递 ID 令牌,它是整个用户数据的编码版本,然后 Firebase 服务器可以解码并将auth
变量传递给您的规则。
有关详细信息,请参阅有关对 Firebase 实时数据库的 REST API 请求进行身份验证的文档,特别是有关使用 ID 令牌进行身份验证的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.